package com.handinfo.util;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import java.util.Vector;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;

//#if lucency == "YES"
import android.util.Log;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.game.RoleLifeSkill;
import com.xl.lib.Utils;
//import com.nokia.mid.ui.DirectGraphics;
//import com.nokia.mid.ui.DirectUtils;
//#endif
/**
 * 
 * 工具方法集合类 一些通用的工具方法的收集类
 * 
 * @author Xia Mingwei
 * @version 1.0
 */
public class Tools
{
	// ///////////////////////////////////////////////////////////
	// 调试用常量定义
	public static final int EDITION_DEBUG = -1;//内部测试
	
	public static final int EDITION_HANDINFO = 100;//大地广联
	
	public static final int EDITION_DANGLE = 1101;//当乐
	public static final int EDITION_XIAOYOUXI = 2101;//手机小游戏

	public static final int EDITION_TIANXIA = 2103;//手游天下
	public static final int EDITION_ZHANGQU = 2104;//掌趣
	public static final int EDITION_BAIDU = 2105;//百度
	public static final int EDITION_BAORUAN = 2106;//宝软
	public static final int EDITION_LIQU = 2107;//历趣网
	public static final int EDITION_PAOJIAO = 2108;//泡椒网
	
	
	public static final int EDITION_SHOUYOU = 2102;//17手游
	public static final int EDITION_SHOUYOU1 = 21021;//17手游1
	public static final int EDITION_SHOUYOU2 = 21022;//17手游2
	public static final int EDITION_SHOUYOU3 = 21023;//17手游3
	
	public static final int EDITION_YIWA = 2109;//易蛙
	public static final int EDITION_YIWA1 = 21091;//易蛙1
	public static final int EDITION_YIWA2 = 21092;//易蛙2
	public static final int EDITION_YIWA3 = 21093;//易蛙3
	public static final int EDITION_YIWA4 = 21094;//易蛙4
	public static final int EDITION_YIWA5 = 1103;//易蛙5
	public static final int EDITION_YIWA6 = 11031;//易蛙6
	public static final int EDITION_YIWA7 = 11032;//易蛙7
	public static final int EDITION_YIWA8 = 11033;//易蛙8
	public static final int EDITION_YIWA9 = 11034;//易蛙9
	public static final int EDITION_YIWA10 = 21095;//易蛙10
	public static final int EDITION_YIWA11 = 21096;//易蛙11
	public static final int EDITION_YIWA12 = 21097;//易蛙12
	public static final int EDITION_YIWA13 = 21098;//易蛙13
	public static final int EDITION_YIWA14 = 21099;//易蛙14
	
	//九游渠道
	public static final int EDITION_JIUYOU = 1102;//九游
	public static final int EDITION_JIUYOU_1 = 11021;//九游1
	public static final int EDITION_JIUYOU_2 = 11022;//九游2
	public static final int EDITION_JIUYOU_3 = 11023;//九游3
	public static final int EDITION_JIUYOU_4 = 11024;//九游4
	public static final int EDITION_JIUYOU_5 = 11025;//九游5
	public static final int EDITION_JIUYOU_6 = 11026;//九游6
	
	public static final int EDITION_JIUYOU_M = 1102;//九游
	public static final int EDITION_JIUYOU_1M = 11021;//九游1
	public static final int EDITION_JIUYOU_2M = 11022;//九游2
	public static final int EDITION_JIUYOU_3M = 11023;//九游3
	public static final int EDITION_JIUYOU_4M = 11024;//九游4
	public static final int EDITION_JIUYOU_5M = 11025;//九游5
	public static final int EDITION_JIUYOU_6M = 11026;//九游6
	
	public static final int EDITION_ZHIDIANCHUANMEI1 = 2110;//指点传媒1
	public static final int EDITION_ZHIDIANCHUANMEI2 = 21101;//指点传媒1
	public static final int EDITION_ZHIDIANCHUANMEI3 = 21102;//指点传媒1
	public static final int EDITION_ZHIDIANCHUANMEI4 = 21103;//指点传媒1
	public static final int EDITION_ZHIDIANCHUANMEI5 = 21104;//指点传媒1
	public static final int EDITION_ZHIDIANCHUANMEI6 = 21105;//指点传媒1
	public static final int EDITION_ZHIDIANCHUANMEI7 = 21106;//指点传媒1
	public static final int EDITION_ZHIDIANCHUANMEI8 = 21107;//指点传媒1
	public static final int EDITION_ZHIDIANCHUANMEI9 = 21108;//指点传媒1
	public static final int EDITION_ZHIDIANCHUANMEI10 = 21109;//指点传媒1
	
	public static final int EDITION_LEXUN = 1104;//乐讯
	public static final int EDITION_YIYOUWANG = 1105;//易游网
	public static final int EDITION_FEILIUXIAZAI = 1106;//飞流下载
	
	public static final int EDITION_DADAOZHISHENG = 2111;//大道智胜
	public static final int EDITION_DADAOZHISHENG1 = 21111;//大道智胜1
	
	
	public static final int EDITION_TENGXUN = 9999;//腾讯


//#ifdef edition
//#expand public static final int CHANNEL = EDITION_%edition%;
//#else
	public static final int CHANNEL = EDITION_DEBUG;
//#endif
	
	//#if edition == "DEBUG"
	public final static boolean FAST = false; //跳过剧情
	public final static boolean DEBUG = true; //此为假时,混淆后会删除调试信息以减小JAR包
	public final static boolean DEBUG2 = true; //此为假时,混淆后会删除调试信息以减小JAR包
	public final static boolean EMULATOR_OPEN = true;//是否可以使用模拟器
	public final static boolean TEST = true; //此为假时,为短代版本
	//#elif edition == "HANDINFO"
	//# public final static boolean FAST = false; // 跳过剧情
	//# public final static boolean DEBUG = false; // 此为假时,混淆后会删除调试信息以减小JAR包
	//# public final static boolean DEBUG2 = false; // 此为假时,混淆后会删除调试信息以减小JAR包
	//# public final static boolean EMULATOR_OPEN = true;//是否可以使用模拟器
	//# public final static boolean TEST = true; // 此为假时,为短代版本
	//#else
	//# public final static boolean FAST = false; // 跳过剧情
	//# public final static boolean DEBUG = false; // 此为假时,混淆后会删除调试信息以减小JAR包
	//# public final static boolean DEBUG2 = false; // 此为假时,混淆后会删除调试信息以减小JAR包
	//# public final static boolean EMULATOR_OPEN = false;//是否可以使用模拟器
	//# public final static boolean TEST = true; // 此为假时,为短代版本
	//#endif
	

	public static Vector m_debug_log = new Vector();
	public static boolean m_open_log = false;// 是否开启debug信息

	public static final void debugPrintln(String string)
	{
		if (DEBUG) {
			System.out.println(string);
			m_debug_log.addElement(string);
			if (m_debug_log.size() > 100) {
				m_debug_log.removeElementAt(0);
			}
		}
	}

	public static final void debug(Exception ex)
	{
		if (DEBUG2) {
			ex.printStackTrace();
		}
		else {
			debugPrintln(ex.toString());
			debugPrintln(ex.getMessage());
		}
	}

	public static void showErrorMessage(String message, Exception e)
	{
		
	}

	public static void showErrorMessage(String message)
	{

	}

	public static final char CHANGE_ROW = '`';

	// 对齐方式
	public static final int BOTTOM = Graphics.BOTTOM;

	public static final int HCENTER = Graphics.HCENTER;

	public static final int LEFT = Graphics.LEFT;

	public static final int RIGHT = Graphics.RIGHT;

	public static final int TOP = Graphics.TOP;

	public static final int VCENTER = Graphics.VCENTER;

	public static final int LEFT_TOP = LEFT | TOP;

	public static final int LEFT_VCENTER = LEFT | VCENTER;

	public static final int LEFT_BOTTOM = LEFT | BOTTOM;

	public static final int HCENTER_TOP = HCENTER | TOP;

	public static final int HCENTER_VCENTER = HCENTER | VCENTER;

	public static final int HCENTER_BOTTOM = HCENTER | BOTTOM;

	public static final int RIGHT_TOP = RIGHT | TOP;

	public static final int RIGHT_VCENTER = RIGHT | VCENTER;

	public static final int RIGHT_BOTTOM = RIGHT | BOTTOM;

	/**
	 * 随机
	 */
	static public Random RandomMain = new Random();

	/**
	 * 得到一个随机数
	 * 
	 * end应大于等于start
	 * 
	 * @param start
	 * @param end
	 */
	final static public int getRandom(int start, int end)
	{
		return ((RandomMain.nextInt() >>> 1) % (end - start + 1)) + start;
	}

	// ///////////////////////////////////////////////////////////
	// PNG图片处理代码块开始……
	/**
	 * 底层创建图片
	 * 
	 * @param path String 路径
	 * @return Image 图片
	 */
	public static Image createImage(String path)
	{
		// Image image = null;
		// try {
		// image = Image.createImage(path);
		// }
		// catch (IOException ex) {
		// showErrorMessage("CreatImage error!|path = " + path, ex);
		// }
		// finally {
		// if (image == null) {
		// showErrorMessage("CreatImage error!|path = " + path);
		// }
		// }
		return createBinImage(path);
	}

	public static Image createBinImage(String path)
	{
		if (path == null) {
			return null;
		}
//		try {
//			return Image.createImage(path);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		InputStream is = Utils.getResourceAsStream(path);
//		InputStream is = path.getClass().getResourceAsStream(path);
		return createBinImage(new DataInputStream(is));
	}
	
	
	public static byte[] readInputStream(DataInputStream dis) {
		ByteArrayOutputStream bao = new ByteArrayOutputStream();
		byte[] buffer = new byte[4096];
		int allread = 0;
		try {
			while (true) {
				int readlen;

				readlen = dis.read(buffer);

				if (readlen == -1)
					break;
				bao.write(buffer, 0, readlen);
				allread += readlen;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			dis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bao.toByteArray();
	}

	public static Image createByteImage(byte[] baodata) {
		byte[] data;
		if (baodata[0] == (byte) 0x89) {// 如果是头文件信息，肯定是服务器数据
//			startIndex = 8;
//			len = size;
			data = baodata;
			Log.d("Png", "truePng,baodata[0]="+baodata[0]+",size="+baodata.length);
		}
		else {//如果是客户端文件
			Log.d("Png", "falsePng, baodata[0]="+baodata[0]);
			int len = baodata.length + 20;
			data = new byte[len];
			System.arraycopy(baodata, 0, data, 8, baodata.length);
//			startIndex = 0;
//			len = size;
//			int len = size + 20;
			
			data[0] = (byte) 0x89;
			data[1] = 0x50;
			data[2] = 0x4e;
			data[3] = 0x47;
			data[4] = 0x0d;
			data[5] = 0x0A;
			data[6] = 0x1a;
			data[7] = 0x0a;
			//
			data[len - 1] = (byte) 0x82;
			data[len - 2] = 0x60;
			data[len - 3] = 0x42;
			data[len - 4] = (byte) 0xae;
			data[len - 5] = 0x44;
			data[len - 6] = 0x4e;
			data[len - 7] = 0x45;
			data[len - 8] = 0x49;
			data[len - 9] = 0x00;
			data[len - 10] = 0x00;
			data[len - 11] = 0x00;
			data[len - 12] = 0x00;
		}
	
		if (data != null) {
			return Image.createImage(data,0,data.length);
		}
		return null;
	}
	
	public static Image createBinImage(DataInputStream dis)
	{
		if (dis == null) {
			return null;
		}
//		byte[] data = null;
//		int startIndex = 0;//其实位置
//		int len = 0;//长度
//		try {
//			int size = dis.available();
//			data = new byte[size + 20];
//			ByteArrayOutputStream bao = new ByteArrayOutputStream();
//			byte[] buffer = new byte[4096];
//			int allread = 0;
//			while(true) {
//				int readlen = dis.read(buffer);
//				if (readlen == -1)
//					break;
//				bao.write(buffer, 0, readlen);
//				allread += readlen;
//			}
		byte[] baodata = readInputStream(dis);
		if (baodata != null && baodata.length > 20) {
			return createByteImage(baodata);
		} else {
			return null;
		}
//			if (baodata[0] == (byte) 0x89) {// 如果是头文件信息，肯定是服务器数据
////				startIndex = 8;
////				len = size;
//				data = baodata;
//				Log.d("Png", "truePng,data[0]="+data[0]+",size="+size);
//			}
//			else {//如果是客户端文件
//				Log.d("Png", "falsePng, data[0]="+data[0]);
//				int len = baodata.length + 20;
//				data = new byte[len];
//				System.arraycopy(baodata, 0, data, 8, baodata.length);
////				startIndex = 0;
////				len = size;
////				int len = size + 20;
//				
//				data[0] = (byte) 0x89;
//				data[1] = 0x50;
//				data[2] = 0x4e;
//				data[3] = 0x47;
//				data[4] = 0x0d;
//				data[5] = 0x0A;
//				data[6] = 0x1a;
//				data[7] = 0x0a;
//				//
//				data[len - 1] = (byte) 0x82;
//				data[len - 2] = 0x60;
//				data[len - 3] = 0x42;
//				data[len - 4] = (byte) 0xae;
//				data[len - 5] = 0x44;
//				data[len - 6] = 0x4e;
//				data[len - 7] = 0x45;
//				data[len - 8] = 0x49;
//				data[len - 9] = 0x00;
//				data[len - 10] = 0x00;
//				data[len - 11] = 0x00;
//				data[len - 12] = 0x00;
//			}
//		}
//		catch (IOException e) {
//			e.printStackTrace();
//		}
//		finally {
//			try {
//				dis.close();
//			}
//			catch (IOException e) {
//				showErrorMessage("CreatImage error!|path = ", e);
//			}
//		}
//		if (data != null) {
//			return Image.createImage(data,0,data.length);
//		}
//		return null;
	}

	/**
	 * 获得pngData数组中 关键字为key的数据块儿的开始索引
	 * 
	 * @param pngData byte[]
	 * @param key int
	 * @return int
	 */
	public int getCritialStart(byte[] pngData, int key)
	{
		int i, j = 1;
		for (i = 33; i < pngData.length; i += j) {
			long flag = byteForLong(pngData[i + 4], pngData[i + 5],
					pngData[i + 6], pngData[i + 7]);
			if ((flag & 0xffffffff) != key) {
				long offset = byteForLong(pngData[i], pngData[i + 1],
						pngData[i + 2], pngData[i + 3]);
				j = (int) (offset + 12);
			}
			else {
				s_colorCount = (int) byteForLong(pngData[i], pngData[i + 1],
						pngData[i + 2], pngData[i + 3]);
				return i + 8;
			}
		}
		return 0;
	}

	/**
	 * 获取调色板的开始索引
	 * 
	 * @param pngData byte[]
	 * @return int
	 */
	public static int getColorStart(byte[] pngData)
	{
		int i, j = 1;
		for (i = 33; i < pngData.length; i += j) {
			long flag = byteForLong(pngData[i + 4], pngData[i + 5],
					pngData[i + 6], pngData[i + 7]);
			if ((flag & 0xffffffff) != 0x504c5445) {
				long offset = byteForLong(pngData[i], pngData[i + 1],
						pngData[i + 2], pngData[i + 3]);
				j = (int) (offset + 12);
			}
			else {
				s_colorCount = (int) byteForLong(pngData[i], pngData[i + 1],
						pngData[i + 2], pngData[i + 3]);
				return i + 8;
			}
		}
		return 0;
	}

	/**
	 * 返回abcd四个byte数据组成的long型数据
	 * 
	 * @param a byte
	 * @param b byte
	 * @param c byte
	 * @param d byte
	 * @return long
	 */
	public static long byteForLong(byte a, byte b, byte c, byte d)
	{
		return ((int) (a & 0xff) << 24) + ((int) (b & 0xff) << 16)
				+ ((int) (c & 0xff) << 8) + (int) (d & 0xff);
	}

	/**
	 * 返回abc四个byte数据组成的long型数据
	 * 
	 * @param a byte
	 * @param b byte
	 * @param c byte
	 * @return long
	 */
	public static long byteForLong2(byte a, byte b, byte c)
	{
		return ((int) (a & 0xff) << 16) + ((int) (b & 0xff) << 8)
				+ (int) (c & 0xff);
	}

	public static int s_colorCount = 0;

	/**
	 * 从文件srcFile中获取byte数组
	 * 
	 * @param srcFile String
	 * @return byte[]
	 */
	public static byte[] getByteArrayOfFile(String srcFile)
	{
		byte[] data = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			InputStream is = Utils.getResourceAsStream(srcFile);
//			InputStream is = srcFile.getClass().getResourceAsStream(srcFile);
			int ch = 0;
			while ((ch = is.read()) > -1) {
				bos.write(ch);
			}
			data = bos.toByteArray();
			is.close();
			is = null;
			bos.close();
			bos = null;
		}
		catch (IOException ex) {
			Tools.debug(ex);
		}
		return data;
	}

	/**
	 * 换色主函数 将oldColor数组中的颜色按照索引对应换成newColor数组中的颜色
	 * 
	 * @param srcFile String
	 * @param oldColor int[]
	 * @param newColor int[]
	 * @return Image
	 */
	public static Image changeColor(String srcFile, int[] oldColor,
			int[] newColor)
	{
		byte[] pngData = getByteArrayOfFile(srcFile);
		int colorStart = getColorStart(pngData);
		alterColor(pngData, colorStart, oldColor, newColor);
		long value1 = updateCrcChunk(pngData, colorStart - 4, s_colorCount + 4);
		changeCRC(pngData, colorStart + s_colorCount, value1);
		return (Image.createImage(pngData, 0, pngData.length));
	}

	public static Image changeColor(String srcFile, int type, int addColor,
			int totalFrame, int finalColor)
	{
		byte[] pngData = getByteArrayOfFile(srcFile);
		int colorStart = getColorStart(pngData);
		alterColor(pngData, colorStart, type, addColor, totalFrame, finalColor);
		long value1 = updateCrcChunk(pngData, colorStart - 4, s_colorCount + 4);
		changeCRC(pngData, colorStart + s_colorCount, value1);
		return (Image.createImage(pngData, 0, pngData.length));
	}

	public static Image changeColor(String srcFile, int palStart,
			byte[] paletteData)
	{
		byte[] pngData = getByteArrayOfFile(srcFile);
		for (int i = 0; i < paletteData.length; i++) {
			pngData[i + palStart] = paletteData[i];
		}
		return Image.createImage(pngData, 0, pngData.length);
	}

	/**
	 * 计算CRC校验查询表
	 * 
	 * @return long[]
	 */
	private static long[] getCrcTable()
	{
		int c;
		int n, k;
		long[] crc_table = new long[256];
		for (n = 0; n < 256; n++) {
			c = n;
			for (k = 0; k < 8; k++) {
				if ((c & 1) == 1) {
					c = 0xedb88320 ^ (c >>> 1);
				}
				else {
					c = c >>> 1;
				}
			}
			crc_table[n] = c & 0xffffffffL; // 换为无符号的,与原表对应
		}
		return crc_table;
	}

	/**
	 * 计算新的crc校验码
	 * 
	 * @param buf byte[]
	 * @param start int
	 * @param len int
	 * @return long
	 */
	public static long updateCrcChunk(byte[] buf, int start, int len)
	{
		long c = 0xFFFFFFFFL;
		long[] crc_32_tab = getCrcTable();
		for (int i = start; i < start + len; i++) {
			c = (crc_32_tab[(int) ((c ^ buf[i]) & 0xFF)] ^ (c >> 8));
		}
		return (c ^ 0xFFFFFFFFL);
	}

	/**
	 * 更新从start开始的调色板的数据
	 * 
	 * @param pngData byte[]
	 * @param start int
	 * @param color1 int[]
	 * @param color2 int[]
	 */
	public static void alterColor(byte[] pngData, int start, int[] color1,
			int[] color2)
	{
		for (int i = 0; i < s_colorCount; i += 3) {
			int temp = (int) byteForLong2(pngData[start + i], pngData[start + i
					+ 1], pngData[start + i + 2]);
			for (int j = 0; j < color1.length; j++) {
				if (temp == color1[j]) {
					pngData[start + i] = (byte) ((color2[j] >> 16) & 0xff);
					pngData[start + i + 1] = (byte) ((color2[j] >> 8) & 0xff);
					pngData[start + i + 2] = (byte) (color2[j] & 0xff);
				}
			}
		}
	}

	public final static String[] EFFECT_NAME = { "普通", "灰色", "1/3渐白", "2/3渐白",
			"全白", "1/3渐黑", "2/3渐黑", "全黑", "反色", "做旧" };
	public final static int EFFECT_NORMAL = 0;
	public final static int EFFECT_GRAY = 1;
	public final static int EFFECT_13WHITE = 2;
	public final static int EFFECT_23WHITE = 3;
	public final static int EFFECT_WHITE = 4;
	public final static int EFFECT_13BLACK = 5;
	public final static int EFFECT_23BLACK = 6;
	public final static int EFFECT_BLACK = 7;
	public final static int EFFECT_ANTICOLOR = 8;
	public final static int EFFECT_MAKEOLD = 9;

	public static Image updateImg(int effect, String path)
	{
		switch (effect) {
			case EFFECT_NORMAL: {
				return createImage(path);
			}
			case EFFECT_GRAY: {
				return changeColor(path, AC_GREY, 0, 0, 0);
			}
			case EFFECT_13WHITE: {
				return changeColor(path, AC_APPROACH, 1, 3, 0xffffff);
			}
			case EFFECT_23WHITE: {
				return changeColor(path, AC_APPROACH, 2, 3, 0xffffff);
			}
			case EFFECT_WHITE: {
				return changeColor(path, AC_APPROACH, 3, 3, 0xffffff);
			}
			case EFFECT_13BLACK: {
				return changeColor(path, AC_APPROACH, 1, 3, 0x0);
			}
			case EFFECT_23BLACK: {
				return changeColor(path, AC_APPROACH, 2, 3, 0x0);
			}
			case EFFECT_BLACK: {
				return changeColor(path, AC_APPROACH, 3, 3, 0x0);
			}
			case EFFECT_ANTICOLOR: {
				return changeColor(path, AC_ANTICOLOR, 0, 0, 0);
			}
			case EFFECT_MAKEOLD: {
				return changeColor(path, AC_MAKEOLD, 0, 0, 0);
			}
		}
		return null;
	}

	public final static int AC_ADDCOLOR = 0;
	public final static int AC_APPROACH = 1;
	public final static int AC_BANLANCE = 2;
	public final static int AC_ANTICOLOR = 3;
	public final static int AC_GREY = 4;
	public final static int AC_CLIQUE = 5;
	public final static int AC_INCBRIGHT = 7;
	public final static int AC_DECBRIGHT = 11;
	public final static int AC_MAKEOLD = 12;
	public final static int AC_BLEAK = 13;
	public final static int AC_YELLOW = 14;
	public final static int AC_CHANGE = 15;
	public final static int AC_AFRAID = 16;

	public static void alterColor(byte[] pngData, int start, int type,
			int addColor, int totalFrame, int finalColor)
	{
		switch (type) {
			case AC_ADDCOLOR: { // 添加色调
				for (int i = 0; i < s_colorCount; i += 3) {
					int avg = ((int) (pngData[start + i] & 0xff)
							+ (int) (pngData[start + i + 1] & 0xff) + (int) (pngData[start
							+ i + 2] & 0xff)) / 3;
					int addR = (addColor >> 16) & 0xff;
					int addG = (addColor >> 8) & 0xff;
					int addB = addColor & 0xff;
					pngData[start + i] = (byte) ((255 - addR) * avg / 255 + addR);
					pngData[start + i + 1] = (byte) ((255 - addG) * avg / 255 + addG);
					pngData[start + i + 2] = (byte) ((255 - addB) * avg / 255 + addB);
				}
				break;
			}
			case AC_APPROACH: { // 颜色向finalColor逼近
				int R = (int) ((finalColor >> 16) & 0xff);
				int G = (int) ((finalColor >> 8) & 0xff);
				int B = (int) (finalColor & 0xff);
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					pngData[start + i] = (byte) ((R - r) * addColor / totalFrame + r);
					pngData[start + i + 1] = (byte) ((G - g) * addColor
							/ totalFrame + g);
					pngData[start + i + 2] = (byte) ((B - b) * addColor
							/ totalFrame + b);
				}
				break;
			}
			case AC_BANLANCE: { // 对比度
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					r = ((256 + addColor) * (256 + addColor) * (r - 128) >> 16) + 128;
					g = ((256 + addColor) * (256 + addColor) * (g - 128) >> 16) + 128;
					b = ((256 + addColor) * (256 + addColor) * (b - 128) >> 16) + 128;
					pngData[start + i] = (byte) (limit(r, 0, 255));
					pngData[start + i + 1] = (byte) (limit(g, 0, 255));
					pngData[start + i + 2] = (byte) (limit(b, 0, 255));
				}
				break;
			}
			case AC_ANTICOLOR: { // 反色 底片效果
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] ^ 0xff);
					int g = (int) (pngData[start + i + 1] ^ 0xff);
					int b = (int) (pngData[start + i + 2] ^ 0xff);
					pngData[start + i] = (byte) (r);
					pngData[start + i + 1] = (byte) (g);
					pngData[start + i + 2] = (byte) (b);
				}
				break;
			}
			case AC_GREY: { // 彩色换成灰色
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					int c = (r + g + b) / 3;
					pngData[start + i] = (byte) (c);
					pngData[start + i + 1] = (byte) (c);
					pngData[start + i + 2] = (byte) (c);
				}
				break;
			}
			case AC_CLIQUE: { // 阀值处理 低于阀值为黑色 高于阀值为白色
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					if (r + g + b <= addColor) {
						pngData[start + i] = (byte) (0);
						pngData[start + i + 1] = (byte) (0);
						pngData[start + i + 2] = (byte) (0);
					}
					else {
						pngData[start + i] = (byte) (0xff);
						pngData[start + i + 1] = (byte) (0xff);
						pngData[start + i + 2] = (byte) (0xff);
					}
				}
				break;
			}
			case 6: { // 阀值以上变成灰色 阀值以下不变
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					if (r + g + b > addColor) {
						int c = (r + g + b) / 3;
						pngData[start + i] = (byte) (c);
						pngData[start + i + 1] = (byte) (c);
						pngData[start + i + 2] = (byte) (c);
					}
				}
				break;
			}
			case AC_INCBRIGHT: {
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					int addR = (addColor >> 16) & 0xff;
					int addG = (addColor >> 8) & 0xff;
					int addB = addColor & 0xff;
					pngData[start + i] = (byte) (limit(r + addR, 0, 255));
					pngData[start + i + 1] = (byte) (limit(g + addG, 0, 255));
					pngData[start + i + 2] = (byte) (limit(b + addB, 0, 255));
				}
				break;
			}
			case 8: { // 将非指定值addColor的颜色全部换成finalColor
				for (int i = 0; i < s_colorCount; i += 3) {
					if (byteForLong2(pngData[start + i], pngData[start + i + 1],
							pngData[start + i + 2]) != addColor) {
						pngData[start + i] = (byte) ((finalColor >> 16) & 0xff);
						pngData[start + i + 1] = (byte) ((finalColor >> 8) & 0xff);
						pngData[start + i + 2] = (byte) (finalColor & 0xff);
					}
				}
				break;
			}
			case 9: { // 将非指定值addColor的颜色全部换成灰色
				for (int i = 0; i < s_colorCount; i += 3) {
					if (byteForLong2(pngData[start + i], pngData[start + i + 1],
							pngData[start + i + 2]) != addColor) {
						int r = (int) (pngData[start + i] & 0xff);
						int g = (int) (pngData[start + i + 1] & 0xff);
						int b = (int) (pngData[start + i + 2] & 0xff);
						int c = (r + g + b) / 3;
						pngData[start + i] = (byte) (c);
						pngData[start + i + 1] = (byte) (c);
						pngData[start + i + 2] = (byte) (c);
					}
				}
				break;
			}
			case 10: { // 颜色变粗
				for (int i = 0; i < s_colorCount; i += 3) {
					if (byteForLong2(pngData[start + i], pngData[start + i + 1],
							pngData[start + i + 2]) != addColor) {
						int r = (int) (pngData[start + i] & 0xff);
						int g = (int) (pngData[start + i + 1] & 0xff);
						int b = (int) (pngData[start + i + 2] & 0xff);
						pngData[start + i] = (byte) (r >> addColor << addColor);
						pngData[start + i + 1] = (byte) (g >> addColor << addColor);
						pngData[start + i + 2] = (byte) (b >> addColor << addColor);
					}
				}
				break;
			}
			case 11: {
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					int addR = (addColor >> 16) & 0xff;
					int addG = (addColor >> 8) & 0xff;
					int addB = addColor & 0xff;
					pngData[start + i] = (byte) (limit(r - addR, 0, 255));
					pngData[start + i + 1] = (byte) (limit(g - addG, 0, 255));
					pngData[start + i + 2] = (byte) (limit(b - addB, 0, 255));
				}
				break;
			}
			case AC_MAKEOLD: {
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					pngData[start + i] = (byte) (limit(
							(25756 * r + 50397 * g + 12386 * b) >> 16, 0, 255));
					pngData[start + i + 1] = (byte) (limit(
							(22872 * r + 44958 * g + 11010 * b) >> 16, 0, 255));
					pngData[start + i + 2] = (byte) (limit(
							(17826 * r + 34996 * g + 8585 * b) >> 16, 0, 255));
				}
				break;
			}
			case AC_BLEAK: {
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					pngData[start + i] = (byte) (r * r / 256);
					pngData[start + i + 1] = (byte) (g * g / 256);
					pngData[start + i + 2] = (byte) (b * b / 256);
				}
				break;
			}
			case AC_YELLOW: {
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					int avg = (r + g + b) / 3;
					pngData[start + i] = (byte) (limit((255 - 0x0) * avg / 255
							+ 0x0, 0, 255));
					pngData[start + i + 1] = (byte) (limit((255 - 0x0) * avg / 255
							+ 0x0, 0, 255));
					pngData[start + i + 2] = (byte) (limit((255 - 0xff) * avg / 255
							+ 0xff, 0, 255));
				}
			}
			case AC_CHANGE: {
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					pngData[start + i] = (byte) (limit(g * b / 256, 0, 255));
					pngData[start + i + 1] = (byte) (limit(r * b / 256, 0, 255));
					pngData[start + i + 2] = (byte) (limit(r * g / 256, 0, 255));
				}
				break;
			}
			case AC_AFRAID: {
				for (int i = 0; i < s_colorCount; i += 3) {
					int r = (int) (pngData[start + i] & 0xff);
					int g = (int) (pngData[start + i + 1] & 0xff);
					int b = (int) (pngData[start + i + 2] & 0xff);
					pngData[start + i] = (byte) (limit(Math.abs(g - b) * (g - b)
							/ 128, 0, 255));
					pngData[start + i + 1] = (byte) (limit(Math.abs(r - b)
							* (r - b) / 128, 0, 255));
					pngData[start + i + 2] = (byte) (limit(Math.abs(r - g)
							* (r - g) / 128, 0, 255));
				}
				break;
			}
		}
	}

	/**
	 * 在pngData中修改crc校验码
	 * 
	 * @param pngData byte[]
	 * @param crcStart int
	 * @param newValue long
	 */
	public static void changeCRC(byte[] pngData, int crcStart, long newValue)
	{
		for (int i = 0; i < 4; i++) {
			pngData[crcStart + i] = (byte) ((newValue >> (24 - 8 * i)) & 0xff);
		}
	}

	// PNG图片处理代码块结束
	// ///////////////////////////////////////////////////////////

	/**
	 * 检查长方形是否与给定的区域碰撞
	 * 
	 * @param oX 给定区域的x坐标
	 * @param oY 给定区域的y坐标
	 * @param oW 区域宽度
	 * @param oH 区域高度
	 * @return 是否碰撞
	 */
	public static boolean collide(int x, int y, int x2, int y2, int oX, int oY,
			int oX2, int oY2)
	{
		return ((x < oX2) && (x2 > oX) && (y < oY2) && (y2 > oY));
	}

	/**
	 * 取得一个限制范围内的数值
	 * 
	 * @param num
	 * @param min
	 * @param max
	 * @return
	 */
	public static int limit(int num, int min, int max)
	{
		if (min > max) {
			int tmp = max;
			max = min;
			min = tmp;
		}
		if (num > max) {
			return max;
		}
		else if (num < min) {
			return min;
		}
		else {
			return num;
		}
	}

	/**
	 * 限制数字在min与max之间
	 * 
	 * @param num int
	 * @param min int
	 * @param max int
	 * @return int
	 */
	public static int limit2(int num, int min, int max)
	{
		if (min > max) {
			int temp = min;
			min = max;
			max = temp;
		}
		if (num > max) {
			return num - (max - min + 1);
		}
		else if (num < min) {
			return num + (max - min + 1);
		}
		else {
			return num;
		}
	}

	public static boolean isInRange(int value, int limit1, int limit2)
	{
		return value >= limit1 && value <= limit2;
	}
	
	/**
	 * 点是否在矩形内
	 * @param x
	 * @param y
	 * @param rect 0:left 1:up 2:right 3:down
	 * @return
	 */
	public static boolean pointInRect(int x, int y, int[] rect)
	{
		if (rect == null || rect.length != 4)
			return false;
		if (x < rect[0] || x > rect[2])
			return false;
		if (y < rect[1] || y > rect[3]) {
			return false;
		}
		return true;
//		if (x >= rect[0] && x <= rect[2] && y >= rect[1] && y <= rect[3])
//			return true;
//		else 
//			return false;
	}
	
	/**
	 * 点是否在矩形内
	 * @param x
	 * @param y
	 * @param rect 0:left 1:up 2:w 3:h
	 * @return
	 */
	public static boolean pointInRectWH(int x, int y, int[] rect)
	{
		if (rect == null || rect.length != 4)
			return false;
		if (x < rect[0] || x > rect[2]+rect[0])
			return false;
		if (y < rect[1] || y > rect[3]+rect[1]) {
			return false;
		}
		return true;
//		if (x >= rect[0] && x <= rect[2] && y >= rect[1] && y <= rect[3])
//			return true;
//		else 
//			return false;
	}

	private static Random rand = new Random(System.currentTimeMillis());

	/**
	 * 返回给定范围内的一个随机数值
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	public static int random(int min, int max)
	{
		if (max < min) {
			int tmp = max;
			max = min;
			min = tmp;
		}
		int num = Math.abs(rand.nextInt()) % (max - min + 1);
		return num + min;
	}

	/**
	 * 返回不小于0且不大于指定数的一个随机数值
	 * 
	 * @param max int 指定数
	 * @return
	 */
	public static int random(int max)
	{
		return Math.abs(rand.nextInt()) % (max + 1);
	}

	/**
	 * 获得重新排序后的数组
	 * 
	 * @param array int[]
	 * @return int[]
	 */
	public static int[] adjustArray(int[] array)
	{
		if (array == null || array.length < 2)
			return array;
		int rand = 0;
		int length = array.length;

		for (int i = 0; i < length; i++) {
			rand = Tools.random(1, length - 1);
			int tmp = array[i];
			array[i] = array[(i + rand) % length];
			array[(i + rand) % length] = tmp;
		}
		return array;
	}

	public static Object[] adjustArray(Object[] array)
	{
		if (array == null || array.length < 2)
			return array;
		int rand = 0;
		int length = array.length;

		for (int i = 0; i < length; i++) {
			rand = Tools.random(1, length - 1);
			Object tmp = array[i];
			array[i] = array[(i + rand) % length];
			array[(i + rand) % length] = tmp;
		}
		return array;
	}

	/**
	 * 两点间距离的平方
	 * 
	 * @param x1 int
	 * @param y1 int
	 * @param x2 int
	 * @param y2 int
	 * @return int
	 */
	public static int distanceSquare(int x1, int y1, int x2, int y2)
	{
		return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
	}

	/**
	 * 计算阶乘
	 * 
	 * @param number int
	 * @return int
	 */
	public static int factorial(int number)
	{
		int result = 1;
		while (number > 0) {
			result *= number--;
		}
		return result;
	}

	/**
	 * 递减数值的累加 如累加：10，9，8，7。。。。
	 * 
	 * @param number int
	 * @param interval int 数值间的间隔
	 * @return int
	 */
	public static int addUp(int number, int interval)
	{
		int result = 0;
		while (number > 0) {
			result += number;
			number -= interval;
		}
		return result;
	}

	/**
	 * 返回指定数在指定数组中的索引
	 * 
	 * @param node byte 指定数
	 * @param nodes byte[] 指定数组
	 * @return int 如果不在数组中的话，返回-1
	 */
	public static int indexOf(byte node, byte[] nodes)
	{
		if (nodes == null)
			return -1;
		for (int i = 0; i < nodes.length; i++) {
			if (node == nodes[i])
				return i;
		}
		return -1;
	}

	/**
	 * 返回指定数在指定数组中的索引
	 * 
	 * @param node byte 指定数
	 * @param nodes byte[] 指定数组
	 * @return int 如果不在数组中的话，返回-1
	 */
	public static int indexOf(short node, short[] nodes)
	{
		if (nodes == null)
			return -1;
		for (int i = 0; i < nodes.length; i++) {
			if (node == nodes[i])
				return i;
		}
		return -1;
	}

	/**
	 * 返回指定数在指定数组中的索引
	 * 
	 * @param node byte 指定数
	 * @param nodes byte[] 指定数组
	 * @return int 如果不在数组中的话，返回-1
	 */
	public static int indexOf(int node, int[] nodes)
	{
		if (nodes == null)
			return -1;
		for (int i = 0; i < nodes.length; i++) {
			if (node == nodes[i])
				return i;
		}
		return -1;
	}

	/**
	 * number的n次方
	 * 
	 * @param number int
	 * @param n int
	 * @return int
	 */
	public static int power(int number, int n)
	{
		if (n == 0) {
			return 1;
		}
		int num = number;
		for (int i = 1; i < n; i++) {
			num *= number;
		}
		return num;
	}

	/**
	 * 判断给定线段L1L2与线段L3L4是否相交
	 * 
	 * @param x1 int
	 * @param y1 int
	 * @param x2 int
	 * @param y2 int
	 * @param x3 int
	 * @param y3 int
	 * @param x4 int
	 * @param y4 int
	 * @return boolean
	 */
	public static boolean isLineIntersect(int x1, int y1, int x2, int y2,
			int x3, int y3, int x4, int y4)
	{
		// 两条线段对应的矩形都为相交，则该两条线段也不会相交
		if (Math.max(x3, x4) < Math.min(x1, x2)
				|| Math.min(x3, x4) > Math.max(x1, x2)
				|| Math.max(y3, y4) < Math.min(y1, y2)
				|| Math.min(y3, y4) > Math.max(y1, y2)) {
			return false;
		}

		// 叉积 p > 0 在顺时针方向, p < 0 在逆时针方向, p == 0 共线
		int p123 = (x3 - x1) * (y2 - y1) - (x2 - x1) * (y3 - y1);
		int p124 = (x4 - x1) * (y2 - y1) - (x2 - x1) * (y4 - y1);
		int p341 = (x1 - x3) * (y4 - y3) - (x4 - x3) * (y1 - y3);
		int p342 = (x2 - x3) * (y4 - y3) - (x4 - x3) * (y2 - y3);

		// p3p4线段的两点在p1p2线段的同侧并且p3p4的端点不在p1p2上
		// p1p2线段的两点在p3p4线段的同侧并且p1p2的端点不在p3p4上
		// 则不相交
		if (((p123 ^ p124) > 0 || (p341 ^ p342) > 0) && p123 != 0 && p124 != 0
				&& p341 != 0 && p342 != 0) {
			return false;
		}
		else {
			return true;
		}
	}

	/**
	 * 返回两线段交点个数
	 * 
	 * @param x1 int
	 * @param y1 int
	 * @param x2 int
	 * @param y2 int
	 * @param x3 int
	 * @param y3 int
	 * @param x4 int
	 * @param y4 int
	 * @return boolean
	 */
	public static int getlineIntersectPointNum(int x1, int y1, int x2, int y2,
			int x3, int y3, int x4, int y4)
	{
		// 两条线段对应的矩形都为相交，则该两条线段也不会相交
		if (Math.max(x3, x4) < Math.min(x1, x2)
				|| Math.min(x3, x4) > Math.max(x1, x2)
				|| Math.max(y3, y4) < Math.min(y1, y2)
				|| Math.min(y3, y4) > Math.max(y1, y2)) {
			return 0;
		}

		// 叉积 p > 0 在顺时针方向, p < 0 在逆时针方向, p == 0 共线
		int p123 = (x3 - x1) * (y2 - y1) - (x2 - x1) * (y3 - y1);
		int p124 = (x4 - x1) * (y2 - y1) - (x2 - x1) * (y4 - y1);
		int p341 = (x1 - x3) * (y4 - y3) - (x4 - x3) * (y1 - y3);
		int p342 = (x2 - x3) * (y4 - y3) - (x4 - x3) * (y2 - y3);

		// p3p4线段的两点在p1p2线段的同侧并且p3p4的端点不在p1p2上
		// p1p2线段的两点在p3p4线段的同侧并且p1p2的端点不在p3p4上
		// 则不相交
		if (((p123 ^ p124) > 0 || (p341 ^ p342) > 0) && p123 != 0 && p124 != 0
				&& p341 != 0 && p342 != 0) {
			return 0;
		}
		else if ((p123 == 0 && p124 == 0) || (p341 == 0 && p342 == 0)) {
			return Integer.MAX_VALUE;
		}
		else {
			return 1;
		}
	}

	/**
	 * 获得两线段交点坐标
	 * 
	 * @param x1 int
	 * @param y1 int
	 * @param x2 int
	 * @param y2 int
	 * @param x3 int
	 * @param y3 int
	 * @param x4 int
	 * @param y4 int
	 * @return short[]
	 */
	public static short[] getLineIntersectPoint(int x1, int y1, int x2, int y2,
			int x3, int y3, int x4, int y4)
	{
		int num = getlineIntersectPointNum(x1, y1, x2, y2, x3, y3, x4, y4);
		if (num == 1) {
			int yl = (x1 - x2) * (y4 - y3) - (y1 - y2) * (x4 - x3);
			int yr = (x4 - x2) * (y4 - y3) * (y1 - y2) + y2 * (x1 - x2)
					* (y4 - y3) - y4 * (x3 - x4) * (y2 - y1);
			int y = yr / yl;
			int xl = (x4 - x3) * (y1 - y2) - (x1 - x2) * (y4 - y3);
			int xr = (y1 - y3) * (x2 - x1) * (x4 - x3) + x3 * (y4 - y3)
					* (x2 - x1) - x1 * (y2 - y1) * (x4 - x3);
			int x = xr / xl;
			return new short[] { (short) x, (short) y };
		}
		else if (num == 0) {
			return null;
		}
		// 共线无数交点，目前不考虑
		else {
			return null;
		}
	}

	/**
	 * 线段L与矩形R是否相交
	 * 
	 * @param lsx int 线段起点端X
	 * @param lsy int 线段起点端Y
	 * @param lex int 线段终点端X
	 * @param ley int 线段终点端Y
	 * @param rx1 int 矩形左
	 * @param ry1 int 矩形上
	 * @param rx2 int 矩形右
	 * @param ry2 int 矩形下
	 * @return boolean
	 */
	public static boolean isLineIntersectRect(int lsx, int lsy, int lex,
			int ley, int rx1, int ry1, int rx2, int ry2)
	{
		if ((rx1 <= lsx && lsx <= rx1 && ry1 <= lsy && lsy <= ry2)
				|| (rx1 <= lex && lex <= rx1 && ry1 <= ley && ley <= ry2))
			return true;

		if (rx1 >= lsx) {
			if (isLineIntersect(lsx, lsy, lex, ley, rx1, ry1, rx1, ry2))
				return true;
		}

		if (rx2 <= lsx) {
			if (isLineIntersect(lsx, lsy, lex, ley, rx2, ry1, rx2, ry2))
				return true;
		}

		if (ry1 >= lsy) {
			if (isLineIntersect(lsx, lsy, lex, ley, rx1, ry1, rx2, ry1))
				return true;
		}

		if (ry2 <= lsy) {
			if (isLineIntersect(lsx, lsy, lex, ley, rx1, ry2, rx2, ry2))
				return true;
		}

		return false;
	}

	/**
	 * 获得线段与矩形最近的交点
	 * 
	 * @param lsx
	 * @param lsy
	 * @param lex
	 * @param ley
	 * @param rx1
	 * @param ry1
	 * @param rx2
	 * @param ry2
	 * @return
	 */
	public static short[] getLineIntersectRectClosestPoint(int lsx, int lsy,
			int lex, int ley, int rx1, int ry1, int rx2, int ry2)
	{
		int distance_min = Integer.MAX_VALUE; // 距离的平方
		int distance_tmp;
		short[] intersectPoint = null;;
		short[] intersectPoint_tmp = null;

		if (rx1 >= lsx) {
			intersectPoint_tmp = getLineIntersectPoint(lsx, lsy, lex, ley, rx1,
					ry1, rx1, ry2);
			if (intersectPoint_tmp != null) {
				distance_tmp = (lsx - intersectPoint_tmp[0])
						* (lsx - intersectPoint_tmp[0])
						+ (lsy - intersectPoint_tmp[1])
						* (lsy - intersectPoint_tmp[1]);
				if (distance_tmp < distance_min) {
					distance_min = distance_tmp;
					intersectPoint = intersectPoint_tmp;
				}
			}
		}

		if (rx2 <= lsx) {
			intersectPoint_tmp = getLineIntersectPoint(lsx, lsy, lex, ley, rx2,
					ry1, rx2, ry2);
			if (intersectPoint_tmp != null) {
				distance_tmp = (lsx - intersectPoint_tmp[0])
						* (lsx - intersectPoint_tmp[0])
						+ (lsy - intersectPoint_tmp[1])
						* (lsy - intersectPoint_tmp[1]);
				if (distance_tmp < distance_min) {
					distance_min = distance_tmp;
					intersectPoint = intersectPoint_tmp;
				}
			}
		}

		if (ry1 >= lsy) {
			intersectPoint_tmp = getLineIntersectPoint(lsx, lsy, lex, ley, rx1,
					ry1, rx2, ry1);
			if (intersectPoint_tmp != null) {
				distance_tmp = (lsx - intersectPoint_tmp[0])
						* (lsx - intersectPoint_tmp[0])
						+ (lsy - intersectPoint_tmp[1])
						* (lsy - intersectPoint_tmp[1]);
				if (distance_tmp < distance_min) {
					distance_min = distance_tmp;
					intersectPoint = intersectPoint_tmp;
				}
			}
		}

		if (ry2 <= lsy) {
			intersectPoint_tmp = getLineIntersectPoint(lsx, lsy, lex, ley, rx1,
					ry2, rx2, ry2);
			if (intersectPoint_tmp != null) {
				distance_tmp = (lsx - intersectPoint_tmp[0])
						* (lsx - intersectPoint_tmp[0])
						+ (lsy - intersectPoint_tmp[1])
						* (lsy - intersectPoint_tmp[1]);
				if (distance_tmp < distance_min) {
					distance_min = distance_tmp;
					intersectPoint = intersectPoint_tmp;
				}
			}
		}

		return intersectPoint;
	}

	/**
	 * 将指定的字符串指定的字符分割为字符串数组
	 * 
	 * @param inf
	 * @param tag 标记字符
	 * @param width 宽度限制，到达宽度则换行
	 * @return
	 */
	public static String[] splitString(String inf, Font font, char tag,
			int width)
	{
		if (inf == null) {
			return null;
		}
		int start = 0, end = 0, length = 0;
		String str = inf;
		int tmp = 1;
		// 计算出分割后数组大小
		while (end < str.length()) {
			if (str.charAt(end) == tag
					|| str.charAt(end) == '\n'
					|| (width != 0 && length + font.charWidth(str.charAt(end)) >= width)) {
				tmp++;
				length = 0;
			}
			length += font.charWidth(str.charAt(end));
			end++;
		}
		String[] s_control = new String[tmp];
		s_control[0] = "";
		tmp = 0;
		end = 0;
		length = 0;
		// 遍历所有字符来进行分割
		while (end < str.length()) {
			while (end < str.length()
					&& str.charAt(end) != tag
					&& str.charAt(end) != '\n'
					&& (width == 0 || (width != 0 && length
							+ font.charWidth(str.charAt(end)) < width))) {
				length += font.charWidth(str.charAt(end));
				end++;
			}
			s_control[tmp] = str.substring(start, end);
			tmp++;
			length = 0;
			if (end < str.length()
					&& (str.charAt(end) == tag || str.charAt(end) == '\n')) {
				end++;
			}
			start = end;
		}
		while (s_control[s_control.length - 1] == null) {
			String[] s_tmp = new String[s_control.length - 1];
			arraycopy(s_control, 0, s_tmp, 0, s_tmp.length);
			s_control = null;
			s_control = s_tmp;
		}
		return s_control;
	}

	/**
	 * 分割字符串
	 * 
	 * @param string
	 * @param regex
	 * @return
	 */
	public static String[] splitString(String string, char regex)
	{
		if (string == null)
			return null;
		if (string.length() == 0)
			return new String[] { string };
		int start = 0, end = 0;
		ArrayList objs = new ArrayList();
		while (end < string.length()) {
			while (end < string.length() && string.charAt(end) != regex) {
				end++;
			}
			String tmp = string.substring(start, end);
			objs.addElement(tmp);
			if (end < string.length() && (string.charAt(end) == regex)) {
				end++;
			}
			start = end;
		}

		String[] strings = new String[objs.size()];
		for (int i = 0; i < strings.length; i++) {
			strings[i] = (String) objs.elementAt(i);
		}
		return strings;
	}

	public static void GC(int time)
	{
		try {
			System.gc();
                        Thread.sleep(time);
		}
		catch (InterruptedException ex) {
		}
	}

	/**
	 * 改变数组大小（short[]）
	 * 
	 * @param array
	 * @param index
	 * @param var
	 */
	public static final short[][] resizeArray(short[][] array, int index,
			int var)
	{
		int len = array.length;
		short[][] tmp = array;
		array = new short[len + var][];
		System.arraycopy(tmp, 0, array, 0, index);
		if (var > 0) {
			System.arraycopy(tmp, index, array, index + var, len - index);
		}
		else {
			System.arraycopy(tmp, index - var, array, index, len + var - index);
		}
		tmp = null;
		return array;
	}

	/**
	 * 改变数组大小（short）
	 * 
	 * @param array
	 * @param index
	 * @param var
	 */
	public static final short[] resizeArray(short[] array, int index, int var)
	{
		int len = array.length;
		short[] tmp = array;
		array = new short[len + var];
		System.arraycopy(tmp, 0, array, 0, index);
		if (var > 0) {
			System.arraycopy(tmp, index, array, index + var, len - index);
		}
		else {
			System.arraycopy(tmp, index - var, array, index, len + var - index);
		}
		tmp = null;
		return array;
	}

	/**
	 * 改变数组大小（byte）
	 * 
	 * @param array
	 * @param index
	 * @param var
	 */
	public static final byte[] resizeArray(byte[] array, int index, int var)
	{
		int len = array.length;
		byte[] tmp = array;
		array = new byte[len + var];
		System.arraycopy(tmp, 0, array, 0, index);
		if (var > 0) {
			System.arraycopy(tmp, index, array, index + var, len - index);
		}
		else {
			System.arraycopy(tmp, index - var, array, index, len + var - index);
		}
		tmp = null;
		return array;
	}

	/**
	 * 改变数组大小（String）
	 * 
	 * @param array
	 * @param index
	 * @param var
	 */
	public static final String[] resizeArray(String[] array, int index, int var)
	{
		int len = array.length;
		String[] tmp = array;
		array = new String[len + var];
		System.arraycopy(tmp, 0, array, 0, index);
		if (var > 0) {
			System.arraycopy(tmp, index, array, index + var, len - index);
		}
		else {
			System.arraycopy(tmp, index - var, array, index, len + var - index);
		}
		tmp = null;
		return array;
	}

	public static final RoleLifeSkill[] resizeArray(RoleLifeSkill[] array,
			int index, int var)
	{
		int len = array.length;
		RoleLifeSkill[] tmp = array;
		array = new RoleLifeSkill[len + var];
		System.arraycopy(tmp, 0, array, 0, index);
		if (var > 0) {
			System.arraycopy(tmp, index, array, index + var, len - index);
		}
		else {
			System.arraycopy(tmp, index - var, array, index, len + var - index);
		}
		tmp = null;
		return array;
	}

	/**
	 * 数组复制
	 * 
	 * @param object
	 * @param _int
	 * @param object2
	 * @param _int3
	 * @param _int4
	 */
	public static void arraycopy(Object object, int _int, Object object2,
			int _int3, int _int4)
	{
		System.arraycopy(object, _int, object2, _int3, _int4);
	}

	/**
	 * 完全复制数组
	 * 
	 * @param src short[] 原数组
	 * @return short[] 复本
	 */
	public static short[] arraycopy(short[] src)
	{
		if (src == null) {
			return new short[0];
		}
		short[] ret = new short[src.length];
		Tools.arraycopy(src, 0, ret, 0, src.length);
		return ret;
	}

	/**
	 * 完全复制数组
	 * 
	 * @param src int[] 原数组
	 * @return int[] 复本
	 */
	public static int[] arraycopy(int[] src)
	{
		if (src == null) {
			return new int[0];
		}
		int[] ret = new int[src.length];
		Tools.arraycopy(src, 0, ret, 0, src.length);
		return ret;
	}

	public static short getShortForObj(Object obj)
	{
		return (short) Integer.parseInt(obj.toString());
	}

	public static byte getByteForObj(Object obj)
	{
		return (byte) Integer.parseInt(obj.toString());
	}

	public static int getIntForObj(Object obj)
	{
		return Integer.parseInt(obj.toString());
	}

	/**
	 * 获取一个值在数组中的第一个位置
	 * 
	 * @param level int 值
	 * @param array int[] 数组
	 * @return int 位置(找不到返回-1)
	 */
	public static int getIndexFromArray(int level, int[] array)
	{
		for (int i = 0; i < array.length; i++) {
			if (level == array[i]) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * 判断给定的数值是否在给定的数组中
	 * 
	 * @param level int
	 * @param array int[]
	 * @return boolean
	 */
	public static boolean isInArray(short value, short[] array)
	{
		for (int i = 0; i < array.length; i++) {
			if (value == array[i]) {
				return true;
			}
		}
		return false;
	}

	public static boolean isInArray(int value, int[] array)
	{
		for (int i = 0; i < array.length; i++) {
			if (value == array[i]) {
				return true;
			}
		}
		return false;
	}

	public static boolean isInArray(long value, long[] array)
	{
		for (int i = 0; i < array.length; i++) {
			if (value == array[i]) {
				return true;
			}
		}
		return false;
	}

	/**
	 * upArrayIndex
	 * 
	 * @param array short[]
	 * @return short[]
	 */
	public static short[] upArrayIndex(short[] array, boolean ismerge)
	{
		if (array == null) {
			return new short[0];
		}
		short[] tmp = new short[array.length];
		System.arraycopy(array, 0, tmp, 0, array.length);
		for (int i = 0; i < tmp.length; i++) {
			for (int j = i + 1; j < tmp.length; j++) {
				if (tmp[i] > tmp[j]) {
					short s = tmp[i];
					tmp[i] = tmp[j];
					tmp[j] = s;
				}
			}
		}
		if (ismerge) {
			short[] tmparray = new short[array.length];
			int count = 0;
			for (int i = 0; i < tmp.length; i++) {
				boolean isadd = true;
				for (int j = 0; j < count; j++) {
					if (tmp[i] == tmparray[j]) {
						isadd = false;
						break;
					}
				}
				if (isadd) {
					tmparray[count++] = tmp[i];
				}
			}
			tmp = new short[count];
			System.arraycopy(tmparray, 0, tmp, 0, count);
		}
		return tmp;
	}

	/**
	 * 合并两个数组
	 * 
	 * @param array1
	 * @param array2
	 * @return
	 */
	public static short[] mergeArray(short[] array1, short[] array2)
	{
		short[] array = new short[array1.length + array2.length];
		System.arraycopy(array1, 0, array, 0, array1.length);
		System.arraycopy(array2, 0, array, array1.length, array2.length);
		return upArrayIndex(array, true);
	}

	public final static int MERGE_INTER = 0; // 交集
	public final static int MERGE_SUF = 1; // 差集
	public final static int MERGE_UNION = 2; // 并集

	/**
	 * 合并两个集合
	 * 
	 * @param sourceVector Vector
	 * @param nonVisMenuVector Vector
	 * @param isMerge boolean
	 * @return Vector
	 */
	public static Vector mergeVector(Vector sourceVector,
			Vector nonVisMenuVector, int type)
	{
		switch (type) {
			case MERGE_SUF: {
				for (int i = 0; i < nonVisMenuVector.size(); i++) {
					int index = sourceVector.indexOf(nonVisMenuVector.elementAt(i));
					if (index != -1) {
						sourceVector.removeElementAt(index);
					}
				}
				break;
			}
			case MERGE_UNION: {
				for (int i = 0; i < nonVisMenuVector.size(); i++) {
					int index = sourceVector.indexOf(nonVisMenuVector.elementAt(i));
					if (index == -1) {
						sourceVector.addElement(nonVisMenuVector.elementAt(i));
					}
				}
				break;
			}
			case MERGE_INTER: {
				for (int i = 0; i < sourceVector.size(); i++) {
					boolean isdel = true;
					for (int j = 0; j < nonVisMenuVector.size(); j++) {
						if (sourceVector.elementAt(i).equals(
								nonVisMenuVector.elementAt(j))) {
							isdel = false;
							break;
						}
					}
					if (isdel) {
						sourceVector.removeElementAt(i);
					}
				}
				break;
			}
		}
		return sourceVector;
	}

	// ///////////////////////////////////////////////////////////
	// 图像绘制处理代码块开始……

	/**
	 * 填充半透明矩形
	 * 
	 * @param g
	 * @param RectX
	 * @param RectY
	 * @param RectWidth
	 * @param RectHeight
	 * @param ARGBColor 0xAARRGGBB
	 */
	public final static void fillRect(Graphics g, int RectX, int RectY,
			int RectWidth, int RectHeight, int ARGBColor)
	{
		if (RectWidth <= 0 || RectHeight <= 0)
			return;
		if ((ARGBColor & 0xff000000) == 0xff000000) {
			g.setColor(ARGBColor);
			g.fillRect(RectX, RectY, RectWidth, RectHeight);
		}
		else if ((ARGBColor & 0xff000000) != 0x00000000) {
			int[] ARGB = new int[RectWidth * RectHeight];
			ARGB[0] = ARGBColor;
			int TempPos = 1;
			while (TempPos < ARGB.length) {
				int TempLen = TempPos;
				if (TempPos + TempLen > ARGB.length) {
					TempLen = ARGB.length - TempPos;
				}
				System.arraycopy(ARGB, 0, ARGB, TempPos, TempLen);
				TempPos += TempLen;
			}
			g.drawRGB(ARGB, 0, RectWidth, RectX, RectY, RectWidth, RectHeight, true);
		}
	}

	public final static void fillArc(Graphics g, int ARGBColor, int x, int y,
			int r, int startAngle, int arcAngle)
	{
		fillArc(g, ARGBColor, x - r / 2, y - r / 2, r, r, startAngle, arcAngle);
	}

	/**
	 * 绘制圆形和椭圆，支持nokiaAPI的可以绘制半透明
	 */
	public final static void fillArc(Graphics g, int ARGBColor, int x, int y,
			int w, int h, int startAngle, int arcAngle)
	{
		//#if lucency == "YES"
//		DirectGraphics dg = DirectUtils.getDirectGraphics(g);
//		dg.setARGBColor(ARGBColor);
     	//#else
		g.setColor(ARGBColor);
		//#endif
		g.fillArc(x, y, w, h, startAngle, arcAngle);
	}

	/**
	 * 获取两个颜色中间的颜色,通常用于渐变绘制
	 * 
	 * @param i int 两个颜色中间的颜色索引
	 * @param num int 要分割的个数
	 * @param color int 开始色
	 * @param selColor int 结束色
	 * @return int 返回需要的颜色值
	 */
	public static int getInterColor(int i, int num, int color, int selColor)
	{
		int sr = (color >> 16) & 0xff;
		int sg = (color >> 8) & 0xff;
		int sb = color & 0xff;
		int er = (selColor >> 16) & 0xff;
		int eg = (selColor >> 8) & 0xff;
		int eb = selColor & 0xff;
		int r = sr + (er - sr) * i / num;
		int g1 = sg + (eg - sg) * i / num;
		int b = sb + (eb - sb) * i / num;
		return (r << 16) + (g1 << 8) + b;
	}

	/**
	 * 获取渐变色数组
	 * 
	 * @param num int 分割个数
	 * @param color int 开始色
	 * @param selColor int 结束色
	 * @return int[] 计算出的渐变数组
	 */
	public static int[] getInterColor(int num, int color, int selColor)
	{
		int[] ret = new int[num];
		for (int i = 0; i < num; i++) {
			ret[i] = getInterColor(i, num, color, selColor);
		}
		return ret;
	}

	public static int temp[] = new int[0];
	public static final int BLUR_LEFT = 0x1;
	public static final int BLUR_RIGHT = 0x2;
	public static final int BLUR_BOTH = BLUR_LEFT | BLUR_RIGHT;

	/**
	 * 绘制带外包围的字符串
	 * 
	 * @param g
	 * @param text 字符串
	 * @param colorb 背景颜色
	 * @param colorf 外框颜色
	 * @param font 字体
	 * @param x 坐标
	 * @param y 坐标
	 * @param dot 锚点
	 */
	public static void drawString(Graphics g, String text, int colorb, int colorf, Font font, int x, int y, int dot)
	{
		g.setColor(colorb);
		g.setFont(font);
		g.drawString(text, x + 2, y + 1, dot);
		g.drawString(text, x + 1, y + 2, dot);
		g.drawString(text, x, y + 1, dot);
		g.drawString(text, x + 1, y, dot);
		g.setColor(colorf);
		g.setFont(font);
		g.drawString(text, x + 1, y + 1, dot);
	}

	public static final void drawString(Graphics g, int color_word,
			int color_shadow, String str, int x, int y, int anchor)
	{
		if (color_shadow != -1) {
			//#if screen == "640360"
			//#else
//			g.setColor(color_shadow);
//			g.drawString(str, x - 1, y, anchor);
//			g.drawString(str, x + 1, y, anchor);
//			g.drawString(str, x, y - 1, anchor);
//			g.drawString(str, x, y + 1, anchor);
//
//			g.drawString(str, x - 1, y - 1, anchor);
//			g.drawString(str, x + 1, y + 1, anchor);
//			g.drawString(str, x + 1, y - 1, anchor);
//			g.drawString(str, x - 1, y + 1, anchor);
			//#endif
		}

		g.setColor(color_word);
		g.drawString(str, x, y, anchor);
	}

	public static final int NUM_SIGN_PLUS = 10;
	public static final int NUM_SIGN_MINUS = 11;

	public static void drawNumber(Graphics g, Image img, int x, int y, int w,
			int h, int number, int interval, int anchor, boolean sign)
	{
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int width = g.getClipWidth();
		int height = g.getClipHeight();
		int num = Math.abs(number);
		byte[] nums = new byte[20];
		int digit = 0;
		for (int i = 0; i < 20; i++) {
			int m = num % 10;
			num /= 10;
			nums[i] = (byte) m;
			if (num == 0) {
				digit = i + 1;
				break;
			}
		}
		int sx = x, sy = y;
		switch (anchor) {
			case Graphics.LEFT | Graphics.BOTTOM:
				sy = y - h;
				break;
			case Graphics.LEFT | Graphics.TOP:
				break;
			case Graphics.RIGHT | Graphics.BOTTOM:
				sx = x - digit * (w + interval);
				sy = y - h;
				break;
			case Graphics.RIGHT | Graphics.TOP:
				sx = x - digit * (w + interval);
				break;
			case Graphics.BOTTOM | Graphics.HCENTER:
				sx = x - digit * (w + interval) / 2;
				sy = y - h;
				break;
			case Graphics.TOP | Graphics.HCENTER:
				sx = x - digit * (w + interval) / 2;
				break;
			case Graphics.HCENTER | Graphics.VCENTER:
				sx = x - digit * (w + interval) / 2;
				sy = y - h / 2;
				break;
		}

		for (int i = 0; i < digit; i++) {
			g.setClip(sx + (digit - i - 1) * (w + interval), sy, w, h);
			g.drawImage(img, sx - nums[i] * w + (digit - i - 1) * (w + interval), sy, Graphics.LEFT | Graphics.TOP);
		}

		if (sign) {
			g.setClip(sx - (w + interval), sy, (w + interval), h);
			if (number > 0) {
				g.drawImage(img, sx - NUM_SIGN_PLUS * w - (w + interval), sy,
						Graphics.LEFT | Graphics.TOP);
			}
			else if (number < 0) {
				g.drawImage(img, sx - NUM_SIGN_MINUS * w - (w + interval), sy,
						Graphics.LEFT | Graphics.TOP);
			}
		}
		g.setClip(clipX, clipY, width, height);
	}

	public static void drawNumber(Graphics g, Image img, int x, int y, int w,
			int h, long number, int interval, int anchor, boolean sign)
	{
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int width = g.getClipWidth();
		int height = g.getClipHeight();
		long num = Math.abs(number);
		byte[] nums = new byte[20];
		int digit = 0;
		for (int i = 0; i < 20; i++) {
			long m = num % 10;
			num /= 10;
			nums[i] = (byte) m;
			if (num == 0) {
				digit = i + 1;
				break;
			}
		}
		int sx = x, sy = y;
		switch (anchor) {
			case Graphics.LEFT | Graphics.BOTTOM:
				sy = y - h;
				break;
			case Graphics.LEFT | Graphics.TOP:
				break;
			case Graphics.RIGHT | Graphics.BOTTOM:
				sx = x - digit * (w + interval);
				sy = y - h;
				break;
			case Graphics.RIGHT | Graphics.TOP:
				sx = x - digit * (w + interval);
				break;
			case Graphics.BOTTOM | Graphics.HCENTER:
				sx = x - digit * (w + interval) / 2;
				sy = y - h;
				break;
			case Graphics.TOP | Graphics.HCENTER:
				sx = x - digit * (w + interval) / 2;
				break;
			case Graphics.HCENTER | Graphics.VCENTER:
				sx = x - digit * (w + interval) / 2;
				sy = y - h / 2;
				break;
		}

		for (int i = 0; i < digit; i++) {
			g.setClip(sx + (digit - i - 1) * (w + interval), sy,
					(w + interval), h);
			g.drawImage(img, sx - nums[i] * w + (digit - i - 1)
					* (w + interval), sy, Graphics.LEFT | Graphics.TOP);
		}

		if (sign) {
			g.setClip(sx - (w + interval), sy, (w + interval), h);
			if (number > 0) {
				g.drawImage(img, sx - NUM_SIGN_PLUS * w - (w + interval), sy,
						Graphics.LEFT | Graphics.TOP);
			}
			else if (number < 0) {
				g.drawImage(img, sx - NUM_SIGN_MINUS * w - (w + interval), sy,
						Graphics.LEFT | Graphics.TOP);
			}
		}
		g.setClip(clipX, clipY, width, height);
	}

	public static void drawImage(Graphics g, Image img, int x, int y, int w,
			int h, int anchor)
	{
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int width = g.getClipWidth();
		int height = g.getClipHeight();

		int sx = x, sy = y;
		switch (anchor) {
			case Graphics.LEFT | Graphics.BOTTOM:
				sy = y - h;
				break;
			case Graphics.LEFT | Graphics.TOP:
				break;
			case Graphics.RIGHT | Graphics.BOTTOM:
				sx = x - w;
				sy = y - h;
				break;
			case Graphics.RIGHT | Graphics.TOP:
				sx = x - w;
				break;
			case Graphics.BOTTOM | Graphics.HCENTER:
				sx = x - w / 2;
				sy = y - h;
				break;
			case Graphics.TOP | Graphics.HCENTER:
				sx = x - w / 2;
				break;
			case Graphics.HCENTER | Graphics.VCENTER:
				sx = x - w / 2;
				sy = y - h / 2;
				break;
		}

		g.setClip(sx, sy, w, h);
		g.drawImage(img, x, y, anchor);

		g.setClip(clipX, clipY, width, height);
	}

	/*
	 * 画模糊效果 src:源图 x,y,width,height:画的区域 blurAmount:模糊度 desAmount:饱和度 blurType:模糊方向
	 */
	public static void paintBlurEffect(Image src, Graphics g, int x, int y,
			int width, int height, int blurAmount, int desAmount, int blurType)
	{
		try {
			int buf[] = temp;
			// dg.getPixels(tempShort, 0, width, 0, 0, width, height, format);
			int d = blurAmount % 128;
			int maxBlock = temp.length / width;
			int block = 80;
			int yy = 0;
			boolean done = false;
			while (!done) {
				block = height - yy;
				if (block > maxBlock)
					block = maxBlock - 1;
				else
					done = true;
				int size = width * block;

				src.getRGB(buf, 0, width, x, y + yy, width, block);
				blurDesaturateBlock(size, buf, d, desAmount, blurType);
				g.drawRGB(buf, 0, width, x, y + yy, width, block, false);
				yy += block;
			}
		}
		catch (Exception e) {
		}
	}

	public static void blurDesaturateBlock(int size, int buf[], int blurAmount,
			int desAmount, int burType)
	{
		int r1 = 0;
		int b1 = 0;
		int r1b1 = 0;
		int g1 = 0;
		// blur in both sides
		// right to left
		if ((burType & BLUR_LEFT) == BLUR_LEFT)
			for (int k = size - 1; k >= 0; k--) {
				int c2 = buf[k];
				int r2b2 = (c2) & 0xFF00FF;
				int g2 = (c2) & 0xFF00;
				r1b1 = (r2b2 + ((r1b1 - r2b2) * blurAmount >> 7)) & 0xFF00FF;
				g1 = (g2 + ((g1 - g2) * blurAmount >> 7)) & 0x00FF00;
				int color = r1b1 | g1;
				buf[k] = color;
			}
		if ((burType & BLUR_RIGHT) == BLUR_RIGHT)
			for (int k = 0; k < size; k++) {
				int c2 = buf[k];
				int r2 = (c2) & 0xFF0000;
				int g2 = (c2) & 0xFF00;
				int b2 = (c2) & 0xFF;
				r1 = r2 + ((r1 - r2) * blurAmount >> 7);
				g1 = g2 + ((g1 - g2) * blurAmount >> 7);
				b1 = b2 + ((b1 - b2) * blurAmount >> 7);
				// the desaturated components
				int lum = ((r1 >> 16) + (g1 >> 8) + b1 + 16) >> 2; // the
				// desaturated
				// col
				// (luminosity)
				int desR = (lum << 16);
				int desG = (lum << 8);
				int desB = (lum);
				desR = r1 + ((desR - r1) * desAmount >> 7);
				desG = g1 + ((desG - g1) * desAmount >> 7);
				desB = b1 + ((desB - b1) * desAmount >> 7);
				int color = (desR & 0xFF0000) | (desG & 0xFF00) | desB;
				buf[k] = color;
			}
	}

	public static Image ZoomImage(Image src, int desW, int desH)
	{
		Image desImg = null;
		int srcW = src.getWidth(); // 原始图像宽
		int srcH = src.getHeight(); // 原始图像高
		int[] srcBuf = new int[srcW * srcH]; // 原始图片像素信息缓存

		src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);

		for (int i = 0; i < srcBuf.length; i++) {
			if ((srcBuf[i] & 0x00ffffff) == 0x00ffffff) {
				srcBuf[i] = (srcBuf[i] & 0x00ffffff);
			}
		}

		// 计算插值表
		int[] tabY = new int[desH];
		int[] tabX = new int[desW];

		int sb = 0;
		int db = 0;
		int tems = 0;
		int temd = 0;
		int distance = srcH > desH ? srcH : desH;
		for (int i = 0; i <= distance; i++) { /* 垂直方向 */
			tabY[db] = sb;
			tems += srcH;
			temd += desH;
			if (tems > distance) {
				tems -= distance;
				sb++;
			}
			if (temd > distance) {
				temd -= distance;
				db++;
			}
		}

		sb = 0;
		db = 0;
		tems = 0;
		temd = 0;
		distance = srcW > desW ? srcW : desW;
		for (int i = 0; i <= distance; i++) { /* 水平方向 */
			tabX[db] = sb;
			tems += srcW;
			temd += desW;
			if (tems > distance) {
				tems -= distance;
				sb++;
			}
			if (temd > distance) {
				temd -= distance;
				db++;
			}
		}

		// 生成放大缩小后图形像素buf
		int[] desBuf = new int[desW * desH];
		int dx = 0;
		int dy = 0;
		int sy = 0;
		int oldy = -1;
		for (int i = 0; i < desH; i++) {
			if (oldy == tabY[i]) {
				System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
			}
			else {
				dx = 0;
				for (int j = 0; j < desW; j++) {
					desBuf[dy + dx] = srcBuf[sy + tabX[j]];
					dx++;
				}
				sy += (tabY[i] - oldy) * srcW;
			}
			oldy = tabY[i];
			dy += desW;
		}

		// 生成图片
		desImg = Image.createRGBImage(desBuf, desW, desH, true);

		return desImg;
	}

	public static final int PARTICLE_COUNT_MAX = 300;
	public static short[][] s_particled;

	/**
	 * 添加雪粒子
	 * 
	 * @param x int 出现位置X
	 * @param y int 出现位置Y
	 * @param vX int 速度X
	 * @param vY int 速度Y
	 * @param size int 粒子大小
	 * @param life int 粒子生命周期
	 */
	public static void addParticle(int x, int y, int vX, int vY, int size,
			int life)
	{
		if (s_particled == null) {
			s_particled = new short[PARTICLE_COUNT_MAX][6];
		}
		for (int i = 0; i < PARTICLE_COUNT_MAX; i++) {
			if (s_particled[i][5] <= 0) {
				s_particled[i][0] = (short) x;
				s_particled[i][1] = (short) y;
				s_particled[i][2] = (short) vX;
				s_particled[i][3] = (short) vY;
				s_particled[i][4] = (short) size;
				s_particled[i][5] = (short) life;
				return;
			}
		}

	}

	/**
	 * 开始下雪
	 * 
	 * @param g Graphics
	 * @param x int 窗口坐标在场景中的位置X
	 * @param y int 窗口坐标在场景中的位置Y
	 * @param w int 窗口宽度
	 * @param h int 窗口高度
	 * @param windSpeed int 风速 （对雨雪有一定的影响）
	 */
	public static void startSnow(Graphics g, int x, int y, int w, int h, int windSpeed, int colour)
	{
		Tools.addParticle(Tools.random(x - AbstractGameScreen.SCREEN_WIDTH / 2,
				x + w + AbstractGameScreen.SCREEN_WIDTH / 2), Tools.random(y
				- AbstractGameScreen.SCREEN_HEIGHT / 2, y + h
				+ AbstractGameScreen.SCREEN_HEIGHT / 2), Tools.random(-2, 2),
				Tools.random(2, 4), Tools.random(3, 5), Tools.random(80, 120));
		Tools.addParticle(Tools.random(x - AbstractGameScreen.SCREEN_WIDTH / 2,
				x + w + AbstractGameScreen.SCREEN_WIDTH / 2), Tools.random(y
				- AbstractGameScreen.SCREEN_HEIGHT / 2, y + h
				+ AbstractGameScreen.SCREEN_HEIGHT / 2), Tools.random(-2, 2),
				Tools.random(2, 4), Tools.random(3, 5), Tools.random(80, 120));

		g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH,
				AbstractGameScreen.SCREEN_HEIGHT);
		g.setColor(colour);
		for (int i = 0; i < PARTICLE_COUNT_MAX; i++) {
			if (s_particled[i][5] <= 0)
				continue;
			g.fillArc(s_particled[i][0] - x, s_particled[i][1] - y, s_particled[i][4], s_particled[i][4], 0, 360);
			s_particled[i][0] += s_particled[i][2];
			s_particled[i][1] += s_particled[i][3];
			s_particled[i][5]--;
			if (s_particled[i][5] <= 0) {
				s_particled[i][5] = 0;
			}
		}
	}

	/**
	 * 开始下雨
	 */
	public static void startRain(Graphics g, int x, int y, int w, int h,
			int windSpeed, int colour)
	{
		for (int i = 0; i < 8; i++) {
			Tools.addParticle(Tools.random(x - AbstractGameScreen.SCREEN_WIDTH
					/ 4, x + w + AbstractGameScreen.SCREEN_WIDTH / 4), Tools
					.random(y - AbstractGameScreen.SCREEN_HEIGHT / 2, y + h
							- AbstractGameScreen.SCREEN_HEIGHT / 4),
					-AbstractGameScreen.SCREEN_HEIGHT / 30,
					AbstractGameScreen.SCREEN_HEIGHT / 15, Tools.random(4, 6),
					20);
		}
		g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH,
				AbstractGameScreen.SCREEN_HEIGHT);
		g.setColor(colour);
		for (int i = 0; i < PARTICLE_COUNT_MAX; i++) {
			if (s_particled[i][5] <= 0)
				continue;
			g.drawLine(s_particled[i][0] - x, s_particled[i][1] - y,
					s_particled[i][0] - x - s_particled[i][4],
					s_particled[i][1] - y + s_particled[i][4] * 2);
			// g.fillArc(s_particled[i][0] - x, s_particled[i][1] - y,
			// s_particled[i][4], s_particled[i][4], 0, 360);
			s_particled[i][0] += s_particled[i][2];
			s_particled[i][1] += s_particled[i][3];
			s_particled[i][5]--;
			if (s_particled[i][5] <= 0) {
				s_particled[i][5] = 0;
			}
		}
	}

	public static void disposeParticles()
	{
		if (s_particled != null) {
			for (int i = 0; i < s_particled.length; i++) {
				s_particled[i] = null;
			}
			s_particled = null;
		}
	}

	// 图像绘制处理代码块结束
	// ///////////////////////////////////////////////////////////

	// ///////////////////////////////////////////////////////////
	// 手机振动处理代码块开始……
	public static void Vibrate(MIDlet midlet, int time)
	{
		Display.getDisplay(midlet).vibrate(time);
	}

	// 手机振动处理代码块结束
	// ///////////////////////////////////////////////////////////

	/********************************** zeng： 获得分割字符串数组 ***********************************************/

	/**
	 * 获得取值范围在最小值与最大值之间的数【min,max】 2012-2-18 上午04:57:31 venizeng
	 * @param num
	 * @param min
	 * @param max
	 * @return
	 */
	public static final int getMidNum(int num, int min, int max)
	{
		if (num <= min) {
			return min;
		}
		else if (num >= max) {
			return max;
		}
		else {
			return num;
		}
	}

	/**
	 * venizeng 获得字符串最大宽度
	 * 
	 * @param menus
	 * @return
	 */
	public static final int getMaxWidth(String[] menus)
	{
		if (menus == null || menus.length == 0) {
			return 0;
		}
		int maxWidth = 0;
		for (int i = 0, length = menus.length; i < length; i++) {
			if (menus[i] == null) {
				continue;
			}
			int tempW = AbstractGameScreen.s_font.stringWidth(menus[i]);
			if (tempW > maxWidth) {
				maxWidth = tempW;
			}
		}
		return maxWidth;
	}

	/**
	 * 获得二进制中第X位，1返回true ，2 返回false 2012-2-24 上午08:16:48 venizeng
	 * @param num
	 * @param index
	 * @return
	 */
	public static final boolean getBitNum(long num, int index)
	{
		for (int i = 0; i < index; i++) {
			num >>= 1;
		}
		return (num & 0x01) == 1;
	}

	public static String[] getStrArray(String content, Font font, char tag,int maxWidth){
		if (content == null) {
			return null ;
		}
		String[] temp = new String[30];
		int rowLength = 0;// 行长度
		char tempChar = 0;
		int start = 0;
		int row = 0;// 行指针
		int charlength = 0;
		char[] chars = content.toCharArray();
		char changeRow = '\n';
		for (int i = 0, size = content.length(); i < size; i++) {
			tempChar = chars[i];
			charlength = font.charWidth(tempChar);
			int sumW = rowLength + charlength;
			if (tempChar == tag || tempChar == changeRow) {// 不能将其放入到字符串中
				if (i <= start) {// 当前就是第一个
					temp[row] = "";
				}
				else {
					temp[row] = content.substring(start, i);
				}
				row++;
				start = i + 1;
				rowLength = 0;
			}
			else if (sumW > maxWidth) {
				temp[row] = content.substring(start, i);
				start = i;
				rowLength = charlength;
				row++;
			}
			else if (sumW == maxWidth) {
				temp[row] = content.substring(start, i);
				row++;
				start = i;
				rowLength = 0;
			}
			else {
				rowLength += charlength;
			}

			if (row >= temp.length) {
				String[] temp2 = new String[temp.length + 10];
				System.arraycopy(temp2, 0, temp, 0, temp.length);
				temp = null;
				temp = temp2;
				temp2 = null;
			}
		}
		if (start < chars.length) {
			temp[row] = content.substring(start, content.length());
		}
		String[] result = null;
		result = new String[row + 1];
		for (int i = 0, length = result.length; i < length; i++) {
			result[i] = temp[i];
		}
		return result;
	}

	/**
	 * 将字符串转换为字符串数组
	 * 
	 * @param content
	 * @param font
	 * @param maxWidth
	 * @return
	 */
	public static String[] getStrArray(String content, Font font, int maxWidth)
	{
		return getStrArray(content, font, CHANGE_ROW, maxWidth);
	}

	/**
	 * 将字符串转换为字符串数组
	 * 
	 * @param content
	 * @param font
	 * @param maxWidth
	 * @return
	 */
	public static String[] getStrArray(String content, int maxWidth)
	{
		return getStrArray(content, AbstractGameScreen.s_font, CHANGE_ROW, maxWidth);
	}

	/**
	 * 将字符串中的字符串修改为新的字符串
	 * 
	 * @param content
	 * @param oldString
	 * @param newString
	 * @return
	 */
	public static String modifyString(String content, String oldString,
			String newString)
	{
		int index = content.indexOf(oldString);
		String pre = content.substring(0, index);
		int length = oldString.length();
		String next = content.substring(length + index, content.length());
		return pre + newString + next;
	}

	/********************************** zeng： 获得分割字符串数组 end ***********************************************/

	public static String[] getObjectArrayExceptSpecObject(String[] objects, String item)
	{
		if (objects == null) {
			return null;
		}
		boolean isExist = false;
		for (int i = 0; i < objects.length; i++) {
			if (objects[i] == item) {
				isExist = true ;
				break ;
			}
		}
		if (!isExist) {
			return objects ;
		}
		if (objects.length == 1 && objects[0].equals(item)) {
			return null;
		}
		String[] newItems = new String[objects.length - 1];
		int index = 0;
		for (int i = 0; i < objects.length; i++) {
			if (objects[i] == item) {
				continue;
			}
			newItems[index] = objects[i];
			index++;
		}
		return newItems;
	}

	// ///////////////////////////////////////////////////////////
	// 翻转区域
	// ///////////////////////////////////////////////////////////
	public static void drawRegion(Graphics g, Image img, int x, int y, int trans, int anchor)
	{
		g.drawRegion(img, 0, 0, img.getWidth(), img.getHeight(), trans, x, y, anchor);
	}

	/**
	 * 图片翻转方式 TRANS_NONE = 0
	 */
	public static final byte TRANS_NONE = 0;

	/**
	 * 图片翻转方式，上下翻转 TRANS_V = 1
	 */
	public static final byte TRANS_V = 1;

	/**
	 * 图片翻转方式，左右翻转 TRANS_H = 2
	 */
	public static final byte TRANS_H = 2;

	/**
	 * 图片翻转方式，旋转180度 TRANS_HV = 3
	 */
	public static final byte TRANS_HV = 3;

	/**
	 * 图片翻转方式，先左右翻转再逆时针翻转90度 TRANS_H90 = 4
	 */
	public static final byte TRANS_H90 = 4;

	/**
	 * 图片翻转方式，顺时针旋转90度 TRANS_270 = 5
	 */
	public static final byte TRANS_270 = 5;

	/**
	 * 图片翻转方式，逆时针90度 TRANS_90 = 6
	 */
	public static final byte TRANS_90 = 6;

	/**
	 * 图片翻转方式，先上下翻转再逆时针旋转90度 TRANS_V90 = 7
	 */
	public static final byte TRANS_V90 = 7;
	// ///////////////////////////////////////////////////////////
	// 颜色区域
	// ///////////////////////////////////////////////////////////
	/** 红色 */
	public static final int RED = 0xFFFF0000;
	/** 黄色 */
	public static final int YELLOW = 0xFFFFFF00;
	/** 紫色 */
	public static final int PURPLE = 0xFFFF00FF;
	/** 黑色 */
	public static final int BLACK = 0xFF000000;
	/** 蓝色 */
	public static final int BLUE = 0xFF0000FF;
	/** 青色 */
	public static final int CYAN = 0xFF000080;
	/** 深灰色 */
	public static final int DARK_GRAY = 0xFFA9A9A9;
	/** 灰色 */
	public static final int GRAY = 0xFF808080;
	/** 绿色 */
	public static final int GREEN = 0xFF00FF00;
	/** 浅灰色 */
	public static final int LIGHT_GRAY = 0xFFD3D3D3;
	/** 洋红色 */
	public static final int MAGENTA = 0xFFFF00FF;
	/** 粉红色 */
	public static final int PINK = 0xFFFFC0CB;
	/** 白色 */
	public static final int WHITE = 0xFFFFFFFF;
	/** 透明色 */
	public static final int ALPHA = 0x00123456;

	/** 特殊字符，换行 */
	public static final char SPECIAL_CHAR = '&';

	// ////////////////////////////////////////////////////////////////
	public static final int abs(int n)
	{
		return n < 0 ? -n : n;
	}

	public static final long abs(long n)
	{
		return n < 0 ? -n : n;
	}

	public static final int BITS = 6;
	public static final int ONE = 1 << BITS;
	public static final int MASK = ONE - 1;
	private static final int FIX = 12 - BITS;
	private static final short[] SINES_X_FP = { 0 >> FIX, 71 >> FIX,
			143 >> FIX, 214 >> FIX, 286 >> FIX, 357 >> FIX, 428 >> FIX,
			499 >> FIX, 570 >> FIX, 641 >> FIX, 711 >> FIX, 782 >> FIX,
			852 >> FIX, 921 >> FIX, 991 >> FIX, 1060 >> FIX, 1129 >> FIX,
			1198 >> FIX, 1266 >> FIX, 1334 >> FIX, 1401 >> FIX, 1468 >> FIX,
			1534 >> FIX, 1600 >> FIX, 1666 >> FIX, 1731 >> FIX, 1796 >> FIX,
			1860 >> FIX, 1923 >> FIX, 1986 >> FIX, 2048 >> FIX, 2110 >> FIX,
			2171 >> FIX, 2231 >> FIX, 2290 >> FIX, 2349 >> FIX, 2408 >> FIX,
			2465 >> FIX, 2522 >> FIX, 2578 >> FIX, 2633 >> FIX, 2687 >> FIX,
			2741 >> FIX, 2793 >> FIX, 2845 >> FIX, 2896 >> FIX, 2946 >> FIX,
			2996 >> FIX, 3044 >> FIX, 3091 >> FIX, 3138 >> FIX, 3183 >> FIX,
			3228 >> FIX, 3271 >> FIX, 3314 >> FIX, 3355 >> FIX, 3396 >> FIX,
			3435 >> FIX, 3474 >> FIX, 3511 >> FIX, 3547 >> FIX, 3582 >> FIX,
			3617 >> FIX, 3650 >> FIX, 3681 >> FIX, 3712 >> FIX, 3742 >> FIX,
			3770 >> FIX, 3798 >> FIX, 3824 >> FIX, 3849 >> FIX, 3873 >> FIX,
			3896 >> FIX, 3917 >> FIX, 3937 >> FIX, 3956 >> FIX, 3974 >> FIX,
			3991 >> FIX, 4006 >> FIX, 4021 >> FIX, 4034 >> FIX, 4046 >> FIX,
			4056 >> FIX, 4065 >> FIX, 4074 >> FIX, 4080 >> FIX, 4086 >> FIX,
			4090 >> FIX, 4094 >> FIX, 4095 >> FIX, 4096 >> FIX };

	public static final int toInt(int nFP)
	{
		return round(nFP) >> BITS;
	}

	public static final int round(int n)
	{
		return (n + (ONE >> 1)) & ~MASK;
	}

	public static final int sin(int angle)
	{
		angle %= 360 << BITS; // 360 degrees
		if (angle < 0) {
			angle += 360 << BITS;
		}
		if (angle <= 90 << BITS) {
			return SINES_X_FP[toInt(angle)];
		}
		else if (angle <= 180 << BITS) {
			return SINES_X_FP[180 - toInt(angle)];
		}
		else if (angle <= 270 << BITS) {
			return -SINES_X_FP[toInt(angle) - 180];
		}
		else {
			return -SINES_X_FP[360 - toInt(angle)];
		}
	}

	public static final int cos(int angle)
	{
		return sin((90 << BITS) - angle);
	}

	/** 文本样式基本 */
	public static final short TEXT_STYLE_NORMAL = 0x0;

	/** 文本样式阴影 */
	public static final short TEXT_STYLE_SHADOW = 0x100;

	/** 文本样式围绕 */
	public static final short TEXT_STYLE_AROUND = 0x1ef;

	/** 文本样式十字 */
	public static final short TEXT_STYLE_CROSS = 0xaa;

	/** 文本样式带框 */
	public static final short TEXT_STYLE_RIM = (short) 0x8000;

	/**
	 * 绘制矩形，支持半透明
	 * 
	 * @param x 矩形框的左上角坐标X
	 * 
	 * @param y 矩形框的左上角坐标Y
	 * 
	 * @param width 矩形框的宽度
	 * 
	 * @param height 矩形框的高度
	 * 
	 * @param argbColor 矩形框的颜色
	 */
	public static final void drawRectAlpha(Graphics g, int x, int y, int width,
			int height, int argbColor)
	{
		if ((argbColor & 0xff000000) == 0xff000000) {
			g.setColor(argbColor);
			g.fillRect(x, y, width, height);
		}
		else if ((argbColor & 0xff000000) != 0x00000000) {
			int[] argb = new int[width];
			argb[0] = argbColor;
			for (int tempPos = 1; tempPos < argb.length; tempPos <<= 0) {
				System.arraycopy(argb, 0, argb, tempPos,
						(tempPos << 1) > argb.length ? argb.length - tempPos
								: tempPos);
			}

			try {
				g.drawRGB(argb, 0, 0, x, y, width, height, true);
			}
			catch (RuntimeException e) {
				for (int i = height; --i >= 0;) {
					g.drawRGB(argb, 0, width, x, y + i, width, 1, true);
				}
			}
		}
	}

	/**
	 * 显示字符串，可以调整对其方式，可以带效果
	 * 
	 * @param text 要显示的字符串
	 * 
	 * @param color 显示的颜色
	 * 
	 * @param shadowColor 效果颜色
	 * 
	 * @param rectX 对齐区域坐标X
	 * 
	 * @param rectY 对齐区域坐标Y
	 * 
	 * @param rectWidth 对齐区域宽度
	 * 
	 * @param rectHeight 对齐区域高度
	 * 
	 * @param textStyle 效果方式
	 * 
	 * @param posStyle 对其方式
	 */
	public static final void drawStringAdjustable(Graphics g, String text,
			int color, int shadowColor, int rectX, int rectY, int rectWidth,
			int rectHeight, int textStyle, int posStyle)
	{
		drawString(g, text,//
				getPosAdjustableH(AbstractGameScreen.s_font.stringWidth(text),
						rectX, rectWidth, posStyle),//
				getPosAdjustableV(AbstractGameScreen.FONT_H, rectY, rectHeight,
						posStyle),//
				color, shadowColor, textStyle);
	}

	public static final int getPosAdjustableH(int width, int rectX,
			int rectWidth, int posStyle)
	{
		return (posStyle & LEFT) != 0 ? rectX : //
				(posStyle & RIGHT) != 0 ? rectX + rectWidth - width : //
						rectX + (rectWidth - width >> 1);
	}

	public static final int getPosAdjustableV(int height, int rectY,
			int rectHeight, int posStyle)
	{
		return (posStyle & TOP) != 0 ? rectY : //
				(posStyle & BOTTOM) != 0 ? rectY + rectHeight - height : //
						rectY + (rectHeight - height >> 1);
	}

	public static final int getShakePos(int offsetMax, int direct,
			int timerMax, int timer)
	{
		return (direct < 0 ? -offsetMax : offsetMax) * timer / timerMax;
	}

	/**
	 * 显示字符串，可以带效果
	 * 
	 * @param text 要显示的字符串
	 * 
	 * @param stringX 显示的坐标X
	 * 
	 * @param stringY 显示的坐标Y
	 * 
	 * @param color 显示的颜色
	 * 
	 * @param shadowColor 效果颜色
	 * 
	 * @param textStyle 效果样式
	 */
	public static final void drawString(Graphics g, String text, int stringX,
			int stringY, int color, int shadowColor, int textStyle)
	{
		if ((textStyle & TEXT_STYLE_RIM) != 0) {
			// 绘制边框

			int textWidth = AbstractGameScreen.s_font.stringWidth(text);

			drawRectAlpha(g, stringX - 1, stringY - 1, textWidth + 2,
					AbstractGameScreen.FONT_H + 2, shadowColor);

			drawRectRimAlpha(g, stringX - 2, stringY - 2, textWidth + 4,
					AbstractGameScreen.FONT_H + 4, 1, color);

			textStyle &= 1 << 16;
		}

		if (textStyle != TEXT_STYLE_NORMAL) {
			g.setColor(shadowColor);
			for (int ty = stringY - 1; ty <= stringY + 1; ty++) {
				for (int tx = stringX - 1; tx <= stringX + 1; tx++) {
					if ((textStyle & 1) != 0) {
						g.drawString(text, tx, ty, LEFT_TOP);
					}
					textStyle >>= 1;
				}
			}
		}
		g.setColor(color);
		g.drawString(text, stringX, stringY, LEFT_TOP);
	}

	/**
	 * 绘制矩形框
	 * 
	 * @param x 矩形框的左上角坐标X
	 * 
	 * @param y 矩形框的左上角坐标Y
	 * 
	 * @param width 矩形框的宽度
	 * 
	 * @param height 矩形框的高度
	 * 
	 * @param rimWidth 边框的宽度
	 * 
	 * @param argbColor 矩形框的颜色
	 */
	public static final void drawRectRimAlpha(Graphics g, int x, int y,
			int width, int height, int rimWidth, int argbColor)
	{
		drawRectAlpha(g, x, y, width - rimWidth, rimWidth, argbColor);
		drawRectAlpha(g, x + width - rimWidth, y, rimWidth, height - rimWidth,
				argbColor);
		drawRectAlpha(g, x + rimWidth, y + height - rimWidth, width - rimWidth,
				rimWidth, argbColor);
		drawRectAlpha(g, x, y + rimWidth, rimWidth, height - rimWidth,
				argbColor);
	}

	/**
	 * 判断LONG型数的位数
	 * */
	public static int getNumberLongOF(long n)
	{
		n = abs(n); // 加上这一句话，可以判断负数了n);
		if (n < 10) {
			return 1;
		}
		return getNumberLongOF(n / 10) + 1;
	}

	/**
	 * 保存记录
	 * 
	 * @param data 需要保存的数据
	 * @param rmsName 保存的名称
	 * @param id 记录ID
	 */
	public static void saveRecord(byte[] data, String rmsName, int id)
	{
		try {
			RecordStore recordStore = RecordStore.openRecordStore(rmsName, true);
			if (recordStore.getNextRecordID() <= id) { // 无记录
				recordStore.addRecord(data, 0, data.length);
			}
			else { // 有记录时
				recordStore.setRecord(id, data, 0, data.length);
			}
			recordStore.closeRecordStore();
		}
		catch (Exception ex) {
			Tools.debug(ex);
		}
	}

	/**
	 * 读取记录
	 * 
	 * @param rmsName 记录的名称
	 * @param id 记录ID
	 * @return 得到的记录数据
	 */
	public static byte[] loadRecord(String rmsName, int id)
	{
		byte[] tempData = null;
		try {
			RecordStore recordStore = RecordStore.openRecordStore(rmsName, false);
			tempData = recordStore.getRecord(id);
			recordStore.closeRecordStore();
		}
		catch (Exception e) {
			return tempData;
		}
		return tempData;
	}

	/**
	 * 判断是否有记录
	 * 
	 * @param name 记录名称
	 * @return 是否有记录
	 */
	public static boolean haveRecord(String name)
	{
		boolean have = false;
		String list[] = RecordStore.listRecordStores();
		if (list == null) {
			return have;
		}
		int num;
		for (num = list.length; --num >= 0;) {
			if (list[num].equals(name)) {
				have = true;
				break;
			}
		}
		return have;
	}

	public static String replaceAll(String src, String str, String replacement)
	{
		if (src == null || str == null) {
			return src;
		}
		if (replacement == null) {
			replacement = "";
		}
		StringBuffer result = new StringBuffer();
		int idx = 0;
		int begin = 0;
		while ((idx = src.indexOf(str, begin)) > -1) {
			result.append(src.substring(begin, idx)).append(replacement);
			begin = idx + str.length();
		}
		if (begin == 0) {
			return src;
		}
		else if (begin < src.length()) {
			result.append(src.substring(begin));
		}
		return result.toString();
	}

    public static String[] split(String str, String sep) {
        if(str == null){
            return null;
        }
        if(sep == null){
            return new String[]{str};
        }
        Vector v = new Vector();
        int idx = 0;
        int begin = 0;
        while((idx=str.indexOf(sep, begin))>-1){
            v.addElement(str.substring(begin, idx));
            begin = idx + sep.length();
        }
        if(begin > 0 && begin < str.length()){
            v.addElement(str.substring(begin));
        }
        String[] ret = new String[v.size()];
        v.copyInto(ret);
        return ret;
    }
    
    //////////////////////////////////////////////////
//	public static final int //
//			// NOKIA
//			PLATFORM_N7210 = 0, // 128*128 1.0和nokia 无声可振 小于59k
//			PLATFORM_N7260 = 1, // 128*128 2.0 无声可振 小于120k
//			PLATFORM_N6101 = 2,// 128*160
//			PLATFORM_QD = 3, // 176*208 1.0和nokia 无声无振
//			PLATFORM_N7610 = 4, // 176*208 1.0和nokia 可声无振
//			PLATFORM_N5500 = 5,// 208*208
//			PLATFORM_N6230i = 6, // 208*208 2.0 可声可振 小于120k
//			PLATFORM_N73 = 7, // 240*320 1.0和nokia 可声无振
//			PLATFORM_N7370 = 8, // 240*320 2.0 可声可振
//			PLATFORM_E62 = 9, // 320*240 2.0 可声可振
//			// MOTO
//			PLATFORM_C650 = 10, // 128*128- 2.0 可声无振
//			PLATFORM_L6 = 11, // 128*160- 2.0 可声无振
//			PLATFORM_A668 = 12, //
//			PLATFORM_V878 = 13, //
//			PLATFORM_V600 = 14, // 176*220- 2.0 可声无振 内存小于500k
//			PLATFORM_E398 = 15, //
//			PLATFORM_L7 = 16, //
//			PLATFORM_K1 = 17, // 176*220- 2.0 可声无振 内存小于500k 按键功能不同
//			PLATFORM_E680 = 18, // 240*320- 2.0 可声无振 触摸
//			PLATFORM_A760 = 19, //
//			PLATFORM_E6 = 20, //
//			PLATFORM_E2 = 21, // 240*320- 2.0 可声无振 按键映射不同
//			PLATFORM_V8 = 22, //
//			// SE
//			PLATFORM_K300C = 23, // 128*128 2.0 可声可振
//			PLATFORM_K506 = 24, // 128*160
//			PLATFORM_K700C = 25, // 176*220 2.0 可声可振
//			PLATFORM_K790 = 26, // 240*320 2.0 可声可振
//			PLATFORM_S700 = 27, // 240*320 2.0 可声可振
//			PLATFORM_W958C = 28, // 无十字键 无左右软键 触摸
//			// SAMSUNG
//			PLATFORM_E258 = 29, // 128*160 2.0 可声可振 内存小于500k
//			PLATFORM_D508 = 30, // 176*220 2.0 可声可振 内存小于500k
//			PLATFORM_D608 = 31, // 240*320 2.0 可声可振 内存较小
//			PLATFORM_P318 = 32, // 320*240 2.0
//			// NEC
//			PLATFORM_NK = 33, // 176*220 2.0 无声无振 推断内存较小
//			// 不能全屏，左右软键用commandlist识别
//			// PANASONICX
//			PLATFORM_X800 = 34, // 176*208
//			PLATFORM_MX6 = 35, // 240*320 2.0 无声无振 按键映射moto中小版相同
//			// DOPOD
//			PLATFORM_585 = 36, // 176*220
//			// LENOVO
//			PLATFORM_V800 = 37, //
//			// BENQSIEMENS
//			PLATFORM_EL71 = 38, // 240*320 2.0 无声无振
//			// LG
//			PLATFORM_KG77 = 39, // 176*220 2.0 无声无振
//			PLATFORM_KG90n = 40, // 240*320 2.0 无声无振
//			PLATFORM_NOKIA_NOALPHA = 41; // 240*320 无半透
	
    ///////////////////////////////////////////////////////
    // lucency == YES(半透)/NO(非半透)
	// screen == 240320/320240/640360
	// key == MOTO_DEFAULT/MOTO_SPECIAL/NOKIA_DEFAULT/NOKIA_SPECIAL/NOKIA_TOUCH
    ///////////////////////////////////////////////////////
	
	// 机型分类
	public static final int

				PLATFORM_MOTO = 0,//moto键值
				PLATFORM_N73 = 1,// 240 x 320 半透  大内存
				PLATFORM_D608 = 2,// 240 x 320 不半透 大内存
				PLATFORM_320240 = 3,// 320 x 240 半透
				PLATFORM_640360 = 4,// 640 x 360 半透
                PLATFORM_UN73 = 5,
                PLATFORM_UMOTO = 6,//V8
                PLATFORM_E8 = 7,//E8 
	            PLATFORM_E2 = 8,//E2
	            PLATFORM_E72 = 9;//E72
	
//#ifdef platform
//#expand public static final int PLATFORM = PLATFORM_%platform%;
//#else
 	public static final int PLATFORM = PLATFORM_N73;
//#endif
 	
 	
 	/** 半透 */
 	public static final int LUCENCY_YES = 0;
 	/** 不半透 */
 	public static final int LUCENCY_NO = 1;
 	
//#ifdef lucency
//#expand public static final int LUCENCY = LUCENCY_%lucency%;
//#else
 	public static final int LUCENCY = LUCENCY_YES;
//#endif
 	
 	/** 屏幕大小 240x320 */
 	public static final int SCREEN_240320 = 0;
 	/** 屏幕大小 320x240 */
 	public static final int SCREEN_320240 = 1;
 	/** 屏幕大小 640x360 */
 	public static final int SCREEN_640360 = 2;
 	
//#ifdef screen
//#expand public static final int SCREEN = SCREEN_%screen%;
//#else
 	public static final int SCREEN = SCREEN_240320;
//#endif
 	
 	/** moto 默认键值 */
 	public static final int KEY_MOTO_DEFAULT = 0;
 	/** moto 特殊键值 */
 	public static final int KEY_MOTO_SPECIAL = 1;
 	/** nokia 默认键值 */
 	public static final int KEY_NOKIA_DEFAULT = 2;
 	/** nokia 特殊键值 */
 	public static final int KEY_NOKIA_SPECIAL = 3;
 	/** nokia 触摸键值 */
 	public static final int KEY_NOKIA_TOUCH = 4;
 	/** E2 键值 */
 	public static final int KEY_MOTO_E = 5;
 	
//#ifdef key
//#expand public static final int KEY = KEY_%key%;
//#else
 	public static final int KEY = KEY_NOKIA_DEFAULT;
//#endif
 	
 	
    //////////////////////////////////////////////////
    ////屏蔽模拟器
    public static boolean isEmulator(long rmsAmount, long memAmount)
    {
//    	if (rmsAmount == 10000000 && (memAmount == 16252928 || memAmount == 24858624))
    	if (rmsAmount == 10000000)
    		return true;
    	return false;
    }
}
